STL Library---Priority queue

Source: Internet
Author: User

Title Description Description

In an orchard, Toto has beaten down all the fruits and divided them into different heaps according to the different kinds of fruit. A lot decided to synthesize all the fruits.


Each time a merger, a lot can combine the two fruits together, the energy consumed equals two of the weight of the sum of the fruit. It can be seen that all the fruit after the N-1 merger, there is only a pile. The total amount of energy consumed in the merging of fruits equals the physical strength of each merger.


Because it's going to take a lot of effort to bring the fruit home, you need to save as much as you can when you combine the fruit. Assuming that each fruit has a weight of 1, and that the number of fruits and the number of each fruit is known, your task is to design a combination of sequential schemes that will consume the least amount of energy and output this minimum physical cost.


For example, there are 3 kinds of fruit, the number is 1,2,9. The 1 and 2 stacks can be merged first, and the new heap number is 3, which consumes 3 of the energy. Next, the new heap is merged with the original third heap, and a new heap is obtained, with a number of 12, which consumes 12 of the energy. So the total cost of energy =3+12=15. It can be shown that 15 is the minimum physical cost.

Enter a description input Description

The input consists of two lines, the first line being an integer n (1<=n<=10000), which represents the number of species of fruit. The second line contains n integers, separated by spaces, and the first integer AI (1<=ai<=20000) is the number of fruit of the first I.

outputs description output Description

The output includes one row, which contains only an integer, which is the minimum physical cost. The input data guarantees that this valueis less than 2.

sample input to sample

3
1 2 9

Sample output Sample outputs

15

data size & Hint

For 30% of data, n<=1000 is guaranteed:
For 50% of data, n<=5000; is guaranteed
For all data, the n<=10000 is guaranteed.

For the subject, the idea is clear, is to find the quality and the smallest two piles each time, and then merge.

But because of the magnitude of the problem, we can't direct violent search,

So I'm here to use the Priority_queue (priority queue) in the C++stl library.

The so-called priority queue is to prioritize the elements in the queue, so the elements in the queue are sorted by priority.

The queue is the highest or lowest priority, and is inserted into the queue by priority.

#include <queue> #include <functional> #include <cstdio>using namespace Std;int main () {int N;int a[ 10005];p riority_queue<int, Vector<int>, greater<int> >q;scanf ("%d", &n); for (int i = 0; i < n; i+ +) {scanf ("%d", &a[i]); Q.push (A[i]);} Long Long sum = 0;for (int i = 0; i < n-1; i++) {int tmp1 = Q.top (); Q.pop (); int tmp2 = Q.top (); Q.pop (); sum + = Tmp1 + t Mp2;q.push (TMP1 + tmp2);} printf ("%lld\n", sum); return 0;}


STL Library---Priority queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.