NOIP 2004 merger fruit

Luo Gu P1090

https://www.luogu.org/problemnew/show/P1090

JDOJ 1270

Title Description

In an orchard, a lot has shot down all the fruit, and different types of fruit into different piles. We decided to put a lot of fruit all synthetic pile.

Each combined, fruit lots can be combined together piles, consume physical strength equal to the weight of the fruit and piles. As can be seen, all of the fruit through  1-n- n- - After merging 1, on the left a pile. When a lot of fruit combined total consumption of physical strength is equal to the sum of each combined consumed.

But also because these fruits make great efforts to move back home, so a lot to save energy during the merge fruit as possible. Assume that each weight of the fruit are  a number 1, and the number and the type of fruit known in each fruit, your task is to design a combined program order, so that a lot of physical least cost, and outputs the minimum value of the physical cost .

For example  . 3 three kinds of fruit, followed by the number  . 1 2 . 9 9 Can first  . 1 2 2 combined stack, the stack is the new number  . 3 3 is taxing  . 3 3 Subsequently, the new stack and the third stack combined original, and to give the new stack, the number of  12 is . 1 2 is taxing  12 is . 1 2 Therefore, a lot of taxing total  = 12 + 3 = 15 = 3 + . 1 2 = . 1 5 Can prove  15 . 1 5 smallest physical cost value.

Input and output formats

Input formats:

 

A total of two lines.
The first row is an integer of  n-(. 1 \ n-Leq \ 10000 Leq) n- ( . 1 n- . 1 0 0 0 0 ), represents the number of types of fruit.

The second line contains  n- n-integers, separated by spaces, the first  I I integer  a_i (. 1 \ Leq a_i \ Leq 20000) A I ( . 1 A I 2 0 0 0 0 ) is the first  I I kind of fruit Number of.

 

Output formats:

 

An integer, i.e. the value of the minimum physical cost. Ensure that the input data is smaller than  2 ^ {31} 2 . 3 1

 

Sample input and output

Input Sample # 1:  Copy
3 
1 2 9 
Output Sample # 1:  Copy
15

Explanation

For 30% of the data, to ensure that there are n-\ Le 1000 n- . 1 0 0 0:

For 50% of the data, to ensure that there are n-\ Le 5000 n- . 5 0 0 0;

For all of the data, to ensure that there are n-\ 10000 Le n- . 1 0 0 0 0.

 

Read a lot of solution to a problem, the solution is a priority queue that best meetsPsychological weakGreedy connotation and problem-solving ideas. But not on the priority queue are introduced directly just code for novices and children's shoes on the STL do not understand very friendly.


Welfare come!

STL priority queue in the knowledge Glance:

First of all to understand, # include <queue> header file only has this circular queue queue can only use containers, as well as an advanced priority_queue (ie, priority queues) can be used by players A comparison of linear thinking out some "pseudo problem".

The first is a statement

priority_queue<int> q;

<> In what can go into the

There is also a more advanced, C ++ built-tuple, declare as follows:

priority_queue< pair<int,int> > q;

pair <> tuple is a built-in, angle brackets designate a first group of mono-, di-, respectively, of the second element type. Size can be compared to the first element is a first key, a second key for the second element.

What is the priority queue

Priority queue can be understood as a binary heap large root (again).

Well let me explain what the "big root binary heap."

Large root binary heap

Although the name is a bit dirty, but this is a very useful data structure, starting with a binary heap. It is a support insert delete, and query data structures most value. Essentially complete binary tree is a satisfying nature of the "reactor".

Full Nimata 树

Binary nature of much to say. H layer of a binary tree, if it is a complete binary tree, its front h-1 layers are full, and the last one of all leaf nodes (nodes that sterilization) are arranged in order from left to right, that is a complete binary tree.

stack

The basic nature of the heap is actually a complete binary tree, generally the heap is divided into three kinds: big heap root, root heap small and ordinary heap. Large root stack definition: if any of the weights a tree node is less than equal to the weight of its parent node, root stack becomes large. (In layman's terms, that is, the greater the higher up the stack) and vice versa for small roots heap. What ordinary heap, what the name suggests is not a heap.

The basic way to call

insert:

q.push(x);//插入堆

delete:

q.pop();//删除堆顶元素

Inquire:

int x=q.top();//查询堆顶元素(最大值)

Here note the time complexity, insertion and deletion is O (logn), the query is O (1) is.

To achieve a small heap priority queue root

Considering the overloaded operators not very friendly for the novice, here to introduce a more tricky approach: not to say that large root heap priority queue is it? Good Duck! In contrast to the number of I element to be inserted into the stack, the time out can be negated: such as insert 1,2,3, to insert -1, -2, -3, -1 In this case the maximum, the - becomes 1 when taken - (- 1), corresponding to the smallest one removed.


This question I do not need those contents above petty complicated red tape, but since we use the priority queue, it is imperative priority queue thoroughly understand how the matter, find out, you will also have a better understanding of the relevant data structure after then will be thinking more clearly.

 

Code:

#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
int ans;
priority_queue <int,vector<int>,greater<int> >apple;
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int a;
        scanf("%d",&a);
        apple.push(a);
    }
    while(apple.size()>1)
    {
        int x=apple.top();apple.pop();
        int y=apple.top();apple.pop();
        apple.push(x+y);
        years = years + x + y;
    }
    printf("%d",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/fusiwei/p/11166371.html