1063 Merging Fruits
2004 NOIP National League popularity Group
time limit: 1 sspace limit: 128000 KBtitle level: Diamonds Diamond SolvingTitle 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.
Output 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 value is less than 231.
Sample input
Sample Input
3
1 2 9
Sample output
Sample Output
15
Data range and Tips
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.
Category labels
Tags Click here to expand
/*just a drop 60% of the data and think about why Tle*/#include<cstdio>#include<iostream>using namespacestd;inta[10110];Long Longs=0;voidKpintLintR) { intI=l,j=r,mid= (l+r>>1); while(i<=j) { while(A[i]<a[mid]) i++; while(A[j]>a[mid]) j--; if(i<=j) {swap (a[i],a[j]); I++;j--; } } if(j>l) KP (l,j); if(i<R) KP (i,r);}intMain () {intN; CIN>>N; for(intI=1; i<=n;i++) scanf ("%d", A +i); for(intI=1; i<n;i++) {A[i]+=a[i-1]; KP (i,n); S+=a[i]+a[i+1]; } cout<<s<<Endl; return 0;}
Positive solutions: Heap Ordering
#include <cstdio>#include<iostream>using namespacestd;intheap[30002],heap_size,n,ans;voidPutintx) { intNow,next; heap[++heap_size]=x; now=heap_size; while(now>1) {Next=now/2; if(Heap[next]<=heap[now])return; Swap (Heap[next],heap[now]); now=Next; }}int Get(){ intNow,next,res; Res=heap[1]; heap[1]=heap[heap_size--]; now=1; while(now*2<=heap_size) {Next=now*2; if(next1]; if(Heap[now]<=heap[next])returnRes; Swap (Heap[next],heap[now]); now=Next; } returnRes;}intMain () {CIN>>N; for(intI=1, x;i<=n;i++) {scanf ("%d",&x); Put (x); } for(intI=1, x,y;i<n;i++) {x=Get(); Y=Get(); Ans+=x+X; Put (x+y); } cout<<ans<<Endl; return 0;}
1063 Merging Fruits