[HDOJ5933] ArcSoft ' s Office rearrangement (greedy)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5933

Test instructions: Length isnnArray 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 manipulationKKand 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.