Test instructions: Give n heap of fruit, need to merge n heap of fruit into a pile, ask to synthesize all the fruit of the heap to the minimum amount of energy needed to spend
Because you want to minimize the effort, that is, the weight of the pile that needs to be moved each time, you can pick two of the lightest merges, merge and then insert the heap that has not been merged, and repeat the process
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <stack>6#include <vector>7#include <map>8#include <queue>9#include <algorithm>Ten #definemod=1e9+7; One using namespacestd; A -typedefLong LongLL; - the intMain () { -priority_queue<int,vector<int>,greater<int> >PQ; - intn,a,ans=0; -scanf"%d",&n); + for(intI=1; i<=n;i++){ -scanf"%d",&a); + Pq.push (a); A } at - while(Pq.size () >1){ - intx=pq.top ();p q.pop (); - inty=pq.top ();p q.pop (); -ans+=x+y; -Pq.push (x+y); in } -printf"%d\n", ans); to return 0; +}
View Code
Words of hand-written binary pile or wood have written, or use the priority queue written, in Rqnoj handed in, AC100 is over the meaning of it?
TYVJ 1066 Merging Fruit "priority queue"