Combined fruit time limit:1 Sec Memory limit:128 MB
submit:312 solved:113
[Submit] [Status] [Web Board] Description
Now there are n heaps of fruit, and the first heap has AI fruit. Now to merge these fruits into a heap, the cost of each merger is the total fruit count of the two piles of fruit. The minimum cost of merging all fruits.
Input
The first line contains an integer T (t<=50) that represents the number of data groups.
The first row of each group of data contains an integer n (2<=n<=1000) that represents the number of heaps of fruit.
The second line contains n positive integer ai (ai<=100), which represents the number of fruits per heap.
Output
Only one row per group of data represents the minimum consolidation cost.
Sample Input
241 2 3 453 5 2 1 4
Sample Output
1933
HINT
(priority queue + or + Huffman)
#include <stdio.h>#include<queue>using namespacestd;structnode{intA; FriendBOOL operator<(node Aa,node bb) {returnAa.a>BB.A; }};intMain () {intn,t,a,sum; Node Bb,aa; scanf ("%d",&t); while(t--) {priority_queue<node>Q; scanf ("%d",&N); for(intI=0; i<n;i++) {scanf ("%d",&a); BB.A=A; Q.push (BB); } Sum=0; while(!Q.empty ()) {AA=q.top (); Q.pop ();//printf ("%d", aa.a); if(n==1) Break; BB=q.top (); Q.pop (); N--; AA.A+=BB.A; Sum+=aa.a; Q.push (AA); } printf ("%d\n", sum); }} /************************************************************** problem:1588 user:aking2015 language:c++ result:accepted time:92 Ms memory:1064 kb****************************************************************/
Merging fruits (priority queue + or + Huffman)