Title 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 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, 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). input/output format Input Format:
Keyboard input file name. File format:
N (n heap Solitaire, 1 <= n <= 100)
A1 A2 ... An (N heap solitaire, initial number of cards per pile, l<= Ai <=10000) output format:
Output to the screen. The format is:
The minimum number of moves when all heaps are equal. input/Output sample Input Sample # #:
4
9 8 6 Output Sample # #:
3 .
It's like a long time ago, Noip raised the subject of the group.
Test instructions It's okay to understand.
It's easier to do it.
Because the question is given, there must be an optimal solution.
So we'll start by counting all the cards and then judging how many cards are needed for each pile of cards (averaging).
Starting from the first group, as long as the number of cards in this group is not equal to this average
So we can subtract the average from this pile of cards.
(whether positive or negative, positive or negative does not affect the optimal solution, as to why you can think about it)
Transfer the difference to the next set of processing, with the number of steps plus a
Finally, the output of the steps is the answer
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int n,card[105],ave,step;
int main ()
{
scanf ("%d", &n);
for (int i=1;i<=n;++i)
scanf ("%d", &card[i]), ave+=card[i];
ave=ave/n;
for (int i=1;i<=n;++i)
{
if (ave==card[i]) continue;
card[i+1]+=card[i]-ave,step++;
}
printf ("%d\n", step);
return 0;
}