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 of cards, initial number of cards per pile, l<= Ai <=10000) outputs a description of output Description
Output to the screen. The format is:
The minimum number of moves when all heaps are equal. ' Sample input to sample
4
9 8 17 6 sample outputs sample output
3 How to divide the card, when moving cards, as far as possible to move the extra cards to the nearest and missing cards, if the number of cards in excess of the average, then at least to move, if the number of cards less than the average, but also to move at least once, as follows:
There are 4 stacks of cards: ①11②6③15④8
With CNT to save the number of moves, because the average is 10, from left to right to traverse, first 11, because 11-10=1, beyond 1, then from here must move to the right hand. By 6 o'clock, because there is a redundant Part 1, then CNT to add one, indicating that the front to move to the current card in order to ensure that the previous number of cards to meet the requirements. Then, 6+1-10 =-3, which means that by 6 o'clock, there are 3 cards missing. By 15 o'clock, because the front is missing 3 cards, so, CNT plus one, said, to divide some cards to the left, in order to make the front of the cards meet the requirements, at this time, 15-3-10 = 2, said, to 15 o'clock, more than 2 cards, continue, to 8 o'clock, because the front more than 2 cards, so, To move here to make the cards on the left meet the requirements, CNT plus one, at this time, 8+2=10, just, meet the requirements.
Summing up, if the current card, if the number of cards in front of not equal to 0, CNT will add one, said to move more cards, to make the left hand to meet the requirements. The code is as follows:
/* Author: T_rex
title: p1098 Split Solitaire
*/
#include <iostream>
using namespace std;
int c[101] = {0};
void avg () {
int n, i = 0, dist = 0, cnt = 0, sum = 0, av;
CIN >> N;
for (; i < n; i++) {
cin>>c[i];
Sum + = C[i];
}
AV = sum/n;
for (i = 0; i < n; i++) {
if (dist! = 0) cnt++;
C[i] + = dist;
dist = c[i]-av;
}
cout<<cnt<<endl;
}
int main ()
{
avg ();
return 0;
}