"Greedy" evenly split the card

Source: Internet
Author: User

"Greedy" Sharing solitaire time limit: 1 Sec memory limit: up to MB Title Description

There were N piles of cards, numbered 1,2,...,n. There are several sheets on each heap, but the total number of cards must be multiples of n. You can take a number of cards on either heap and move them. The rules for the move are: the cards on the numbered 1 heap can only be moved to the heap numbered 2, and the cards taken on the heap numbered n can only be moved to the heap numbered N-1, and the cards on the other heap may be moved to the adjacent left or right heap. now it's time to find a way to move, with the fewest number of moves to make as many cards as you can on each heap. For example, n=4,4 heap card number is: ①9②8③17④6 Move 3 times can achieve the purpose: from ③ take 4 cards put to ④ (9 8), from ③ take 3 cards put to ② (9), from ② take 1 cards and put them in ① (10 10 10 10).

Input

The input file includes two rows of data. The first action N heap of Cards (1<=n<=100). the second behavior N heap solitaire in each pile of solitaire initial number A1,a2,...,an (l<=ai<=10000).

Output

The only line in the output file, which is the minimum number of moves when all the heaps are equal.

Sample input
49 8 17 6
Sample output
3


1#include <iostream>2 3 using namespacestd;4 intN;5 inta[111];6 intsum,t;7 intavg;8 intMain ()9 {Ten  One      while(cin>>N) A     { -sum=0; t=0; -          for(intI=0; i<n;i++) the         { -Cin>>A[i]; -sum+=A[i]; -         } +avg=sum/N; -          for(intI=0; i<n;i++) +         { A             if(a[i]!=avg) at             { -a[i+1]-=avg-A[i]; -t++; -             } -         } -cout<<t<<Endl; in     } -     return 0; to}

"Greedy" evenly split the card

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.