P1123 Split Solitaire Accepted Tags: greedy noip raise Group 2002
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). format Input 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
The minimum number of moves when all heaps are equal. Example 1 sample input 1[copy]
4
9 8) 17 6
sample Output 1[copy]
3
Limit
1s source per test point
The first problem of NOIP2002 raising group
Code:
#include <cstdio>
using namespace std;
const int maxn=100;
int n,a[maxn+50];
int main ()
{
int i,j,k,ans=0;
scanf ("%d", &n);
for (k=0,i=1;i<=n;i++)
scanf ("%d", &a[i]), k+=a[i];
for (j=k/n,i=1;i<=n;i++) a[i]-=j;
for (i=1;i<n;i++)
if (a[i]!=0) a[i+1]+=a[i],ans++;
printf ("%d\n", ans);
return 0;
}