11079 can move the stone merge
Time limit: 1000MS memory limit: 1000K
Number of submissions: 0 Number of Passes: 0
Question types: programming language: C + +; C VC; Java
Description
There are n heap of stones to form a row (a1,a2,..., An,ai for the number of stones in the heap), it is necessary to merge the stones into a pile, the rule can be selected at least 2 piles at a time to move up to a maximum of K heap removal and then merge, each time the combined score is the new heap stone number. After several merges, the Stones are eventually merged into a pile, scoring the sum of the scores for each merge. Now the solution combines the n heap of stones into a pile of minimum and maximum scores.
Input format
Two lines. The first line N and K, the second row A1 A2 ... an, each AI (1<=i<=n) represents the number of stones in the heap I, n<=100,2<=k<=n.
Output format
Only one row, the lowest score and the highest score for the stone merge, the middle space is connected.
Input sample
7 345 13 12 16 9 5 22
Output sample
199 593
Tips
This problem is solved by greedy algorithm. To label this question the "DP" method is for the students, not the teacher. The rules are not not, but there is a better and faster greedy solution. such as 7 heap of stones, each time can choose a minimum of 2 heap up to 3 heap merges, can be merged as follows: 1th merger: 45+22=67 2nd merger: 67+ 16=83 3rd merger: 83+13=96 4th merger: 96+12=108 5th merger: 108+9=117 6th merge: 117+5=122 combined total score: 67+83+96+108+117+122=593,593 is the maximum score. This can also be combined: the 1th merger: 5+9+12=26 2nd merger: 13+16+22=51 3rd merger: The total value of the 45+51+26=122 merge: 26+51+122=199,199 is the smallest score. So the greedy method of this problem is as follows: (1) guarantee to choose the most two piles, merge until only one pile, to get the maximum score; this is the same as the Huffman tree structure, no longer detailed! (2) To ensure that the minimum number of K-heap, combined until only a pile, so that the minimum score can be obtained. This is the structure of a multi-huffman tree. It is important to note that before merging, if n% (k-1)!=1, the description is merged into the last round, the remainder is not the K heap (but less than the K heap), this is not the minimum score, but must be added before the merger of a number of 0 virtual heap, The purpose of the stack count is to ensure that each time a K-heap merge (including the last one) is combined to the last 1 heaps. Therefore, before merging, do the following://Assuming that the number of stones per heap is placed in stone[1]~stone[n], and Stone[n] up to k-1 number of units is writable; while (n% (k-1)! = 1) { n++; stone[n]=0;}
Author
Zhengchan
My implementation code: where sort () is replaced with insert sort better
#include <iostream> #include <algorithm>using namespace std;/* test data: 7 345 9 5 226 2 3 4 5 6*/int ma In () {int n,k; CIN >> N >> K; int stone[n+10]; for (int i = 0; i < n; i++) {cin >> stone[i]; } int cur; int MinC = 0,MAXC = 0; int mintemp, maxtemp; Sort (stone, stone + N); cur = n-1; Maxtemp = Stone[cur]; while (cur! = 0) {//seek maximum maxtemp = Maxtemp + stone[cur-1]; MaxC + = maxtemp; cur--; }//========================================================================//Assuming that the number of stones per pile is placed in stone[1]~stone[n] , and Stone[n] up to k-1 number of units is writable; while (n% (k-1)! = 1) {stone[n++] = 0; } sort (stone, stone + N); cur = 0; Mintemp = Stone[cur]; while (cur <= n-k) {//Seek minimum for (int i = 0; i < k-1; i++) {stone[cur + k-1] + = Stone[c ur + i]; } MinC + = Stone[cur + k-1]; Cur + = k-1; Sort (stone, stone + N);//for (int i = 0; i < n; i++) {//cout << Stone[i] << "";//}//cout << Endl; } cout << MinC << "<< MaxC; cout << Endl; return 0;}
11079 can move the stone merge (greedy)