C ++ STL: Detailed precedence queue

  Heap is a very important data structure, then how do we be more concise to write large root / root small heap of it?

  For many languages, it can only step by step, hand fighting, but for C ++, the write big root rootlets heap is much more simple, because C ++ has a container called a priority_queue, the container and the queue are included in the header file <queue> in , the priority_queue container called a priority queue can be simulated, the container may be your input data is stored sequentially in the container, insert and delete elements operating elements of the time complexity is log N, but the top of the stack elements of the query (most value) of the time complexity is O (1), insertion and deletion by: a.push (x) and a.pop () to return the operating element is the top of the stack a.top (), since the priority queue own characteristics, its own only write large root heap, but when the data input if we are become its opposite number, then the number of the output is reversed, so that you can be the priority queue into a small heap root, of course, we can also overloaded '< 'to fulfill.

  Here's a related problem of water:

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 Format

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 Format

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 # 1
3 
1 2 9 
Output # 1
15

Description / Tips

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.

 

  This is the problem in 2004 NOIP improve the group, we thought based on greed, each will now have a minimum of two fruit heap combination, we can guarantee that every step is optimal, we can use a small heap root to achieve, each pop-up a minimum of two elements, then they are the fruit of the combined value and then into the stack piled.

Code:

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
priority_queue<int> a;
long long ans=0;
int main(){
    int n,x;    
    cin>>n; 

    for(int i=1;i<=n;i++) cin>>x,a.push(-x);
    for(int i=1;i<n;i++){
        int k=-a.top();
        a.pop();
        int j=-a.top();
        a.pop();
        ans+=k+j;
        a.push(-k-j);
    }        
    cout<<ans<<endl;
}

 

 

 

thanks for reading

 

 

 

  

Guess you like

Origin www.cnblogs.com/tianbowen/p/11390843.html