Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5933
Test instructions: Length isnn
Array of:A_1, a_2, \cdots< Span class= "Mord" >a ? 1 ?? ,a ? 2 ?? ,?
, each operation can either merge two contiguous numbers for one, with a value of two numbers; It is possible to divide a number into two, two numbers and the original number. Transform the array into a length with minimal manipulationKK
and all arrays of equal value, no solution output-1?1
。
Read the wrong question ah ... Test instructions is an adjacent merge, assuming it is an arbitrary merge.
Sub-case discussion, each time to determine the current point is larger or smaller than the AVG, if the big words to put the extra parts on the next point, the demolition is a business-1 times. The remainder merge to the next operation is 2 times (one split, one merge), if less than the average, the entire merge to the previous. Of course consider the current and the next and can do as once.
1#include <bits/stdc++.h>2 using namespacestd;3 4typedefLong LongLL;5 Const intMAXN =100100;6 intN, K;7 LL A[MAXN];8 LL avg;9 Ten intMain () { One //freopen ("in", "R", stdin); A intT, _ =1; -scanf"%d", &T); - while(t--) { thescanf"%d%d",&n,&k); -printf"Case #%d:", _++); -AVG =0; -LL s =0; + for(inti =1; I <= N; i++) { -scanf"%d", &a[i]); +s + =A[i]; A } at if(s% k! =0) { -Puts"-1"); - Continue; - } -AVG = S/K; -LL ret =0; in for(inti =1; I <= N; i++) { - if(A[i] <avg) { to if(A[i] + a[i+1] <=avg) { +ret++; -a[i+1] = A[i] + a[i+1]; the } * Else { $a[i+1] = a[i+1]-avg +A[i];Panax NotoginsengRET + =2; - } the } + Else if(A[i] >avg) { ALL p = a[i]/avg; theLL q = a[i]%avg; + if(Q! =0) { -a[i+1] +=Q; $RET + =2; $ } -RET + = P-1; - } the } -printf"%i64d\n", ret);Wuyi } the return 0; -}
[Hdoj5933]arcsoft's Office rearrangement (greedy)