Playing cards evenly

Source: Internet
Author: User

Title Description Description

There are n stacks of cards, numbered separately, ..., N. There are several sheets on each heap, but the total number of cards must be multiples of N. You can take a card on any heap and move it.
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, the n=4,4 heap of cards are:
①9②8③17④6
Move 3 times to achieve the goal:
Take 4 cards from ③ to ④ (9 8) and take 3 cards from ③ to ② (9), take 1 cards from ② and put them in ① (10 10 10 10).

Enter a description Input Description

First row n (n heap Solitaire, 1 <= n <= 100)
Second line A1 A2 ... An (N heap solitaire, initial number of cards per pile, l<= Ai <=10000)

Output description Output Description

Output to the screen. The format is:
The minimum number of moves when all heaps are equal. ‘

Sample input Sample Input

4
9 8 17 6

Sample output Sample Output

3

Data range and Tips Data Size & Hint

E

Analysis: The topic of the method is a bit misleading, such as example, actually iterate over the array can be, starting from the first number, the number and the average difference how much to find the second number, then the number of steps +1; walk through the number of steps on the answer, beginning to think that there is a bug such as 41 5 28 6 So it is negative Later, I want to give (from the big to the small) the same number of steps. The IQ caught the rush. Code:
/*Author: xtuacm title: p1098 Evenly divided solitaire*/#include<iostream>using namespacestd;intMain () {intn,a[ -],sum=0, step=0; CIN>>N;  for(intI=0; i<n; i++) {cin>>A[i]; Sum+=A[i]; }  intAverage = sum/N;  for(intI=0; i<n; i++)  {    intt =0; if(a[i]!=average) {T= a[i]-average; A[i+1] +=T; Step++; }} cout<<step;}

Playing cards evenly

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.