Nyoj 55 Lazy Xiao Ming (priority queue)

Source: Internet
Author: User

Topic Meaning:

Http://acm.nyist.net/JudgeOnline/problem.php?pid=55

Each time, Xiao Ming can combine the two piles of fruit and consume the same amount of energy as the sum of the weight of two of the fruit. It can be seen that all the fruit after the N-1 merger, there is only a pile. The total energy consumed by xiaoming during the merging of fruits equals the physical strength of each merger.

because it will take a lot of effort to carry these fruits home, so Xiao Ming in the combination of fruit as much as possible to save energy. 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 the sequence, so that xiaoming consumes the least amount of energy, and output this minimum energy consumption value.
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 Xiaoming spent a total of energy =3+12=15. It can be shown that 15 is the minimum physical cost.
Input
The first line of input integer N (0<n<=10) indicates the number of test data groups. Next, each set of test data inputs consists of two lines, the first of which is an integer n (1<=n<=12000), which indicates the number of species of the fruit. The second line contains n integers, separated by spaces, and the first integer AI (1<=ai&lt;=20000) is the number of fruit of the first I.
Output
Each set of test data output includes one row, which contains only an integer, which is the minimum physical cost.
Sample input
13 1 2 9
Sample output
15

Topic Analysis:

If the ability to consume the smallest, each time the smallest two stacked together, so only need to define a priority queue, so that the number of small value priority, each time the two team first removed into the queue, until the end of an element in the queue, the first drink in the process recorded the minimum energy consumption value. You can use the Priority_quque in STL


AC Code:

/** * Priority Queue, */#include <iostream> #include <cstdio> #include <map> #include <cstring> #include <string> #include <algorithm> #include <queue> #include <vector> #include <stack># include<cstdlib> #include <cctype> #include <cstring> #include <cmath> #define LL long longusing    namespace Std;int main () {int t,n,x;    Priority_queue<ll, Vector<ll>, greater<ll> > q;//Define a small number priority high cin>>t;        while (t--) {cin>>n;            for (int i=0;i<n;i++) {cin>>x;        Q.push (x);            } LL a,b,sum=0;//attention precision while (q.size () >1) {a=q.top ();            Q.pop ();            B=q.top ();            Q.pop ();            cout<<a<< "" <<b<<endl;            Q.push (A+B);        Sum=sum+a+b;        } cout<<sum<<endl;    Q.pop (); } return 0;}


Nyoj 55 Lazy Xiao Ming (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.