[NOIP 2002] card sharing

[NOIP 2002] card sharing

topic

Title Description

There are N card stack, are numbered 1,2, ..., N. There are a number of sheets per stack, but the total number of cards will be for the N factor. A plurality of cards can take on either a pile and then moved.

Card moving rules as follows: No. 1 in the card taken heap, the heap can only be moved to the number of 22; the number N of the cards taken heap, the heap can only be moved to the number of N-1; take another heap of cards, can be moved left or right adjacent to the heap.

Now asked to find a mobile method with the least number of moves are the number of cards per heap as much.

E.g. N = 4,44 stack card number are:

①9②8③17④6

3 can achieve the purpose of movement:

4 taken from the card into ③ ④ (9,8,13,10) -> 3 taken from the card into ③ ② (9,11,10,10) -> take the card 11 into ② from ① ( 10,10,10,10).

Input Format

Two lines

The first line: N (N stack of cards, 1≤N≤100)

Second line: A 1, A 2, ..., A n (N stacks of cards, each card number of initial stack, 1≤A i ≤10000)

Output Format

Line: that is, all heap meet the minimum number of moves equal.

Sample input and output

Input # 1 replication
. 4
. 9. 8. 17. 6
output # 1 Copy
3

analysis

First count average.
Analog: the number of such evaluation is 10; the number is read 9814. . . . (Hereinafter the first matter)
then the number of 83 to 14 can, 8-> 11; 91 and 11 give the number, 9-> 10; case 8 is also equal to 10; 2 always necessary operations
can change the angle. Let 8 to 9 first to meet mean, is 9-> 10, 8-> 7, and 8 to 14 to meet the 7-> 10, which also requires two operations
(what the hell. What this does is to .. ...) In doing so, we can go straight from the first number, go to the last number. . It is easy to write.

		再看看 如果读入数是 11 12 7。。。。 也是一样的做法,只不过这次是 11给 12 到自己等于平均数10。
		然后12现在是13了, 13再给7到自己等于10,

Because it reads the number of the last to be turned into average, it can make a few more every time a1, then a2 to the back of a positive number, then later to a less negative (comparable to a2 take a few). And if the average is just a front, then skip, he does not move ah

	应该很清楚了吧。。。。。。。。。

Code

#include<iostream>

using namespace std;

int n;
int a[105];

int main(){
	cin>>n;
	int cnt=0,ans=0;
	for(int i=0;i<n;i++){
		cin>>a[i];
		cnt +=a[i];
	}
	cnt = cnt/n;		//求平均 
	for(int i=0;i<n;i++){
		a[i] = a[i] - cnt;	
	}
	
	for(int i=0;i<n;i++){
		if(a[i] == 0 ){
			continue;
		}
		a[i+1] += a[i];
		ans ++;
	}
	cout<<ans<<endl;
	return 0;
}
Published 75 original articles · won praise 1 · views 3657

Guess you like

Origin blog.csdn.net/A793488316/article/details/104548373