Popular driving range - greedy -P1031 sharing Solitaire

Description Title
of 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 2; 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.

For example, N = 4,4 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) -> taken from a card placed ② ① (10 10 10 10).

Input and output format
input format:

Keyboard input file name. file format:

N (N stack of cards, 1 <= N <= 100)

A1 A2 ... An (N stacks of cards, each card stack initial number, l <= Ai <= 10000)

Output formats:

Output to the screen. The format is:

All stacks have reached the minimum number of moves equal.

Input Output Sample
Input Sample # 1:

4
9 8 17 6

Output Sample # 1:

3
----------------
ideas: sharing subtracting each pile, positive movement, added to a negative number, so that each stack is equal to zero.

#include<iostream>
using namespace std;
int main(){
	int a,p=0,cnt=0;
	cin>>a;
	int q[a];
	for(int j=0;j<a;j++)
	{
		cin>>q[j];
		p += q[j];
	}
	    p /= a ;
	
	for(int j = 0;j < a; j++)
	 q[j] -= p;
	for(int j = 0; j < a; j++)
	{
		if(q[j] == 0)
		continue;
		q[j + 1] += q[j];
		cnt++;
	}
	cout<<cnt<<endl;
	return 0;
} 
Published 80 original articles · won praise 1 · views 1467

Guess you like

Origin blog.csdn.net/zqhf123/article/details/104371041