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, n=4,4 heap card number is: ①9②8③17④6 move 3 times can achieve the purpose: from ③ take 4 cards put to ④ (9 8), from ③ take 3 cards put to ② (9), from ② take 1 Put the card in ① (10 10 10 10). Input
Keyboard input file name. File format:
N (n heap Solitaire, 1 <= n <=) A1 A2 ... An (N heap solitaire, initial number of cards per pile, l<= Ai <=10000) Output
Output to the screen. The format is:
The minimum number of moves when all heaps are equal. Sample Input4 9 8 6Sample Output3Problem-Solving ideas: the topic meaning roughly means that all points must be the same number, with A1 as the starting point, because A1 can only move to the right, if (ANS!=A1) to A2 A1-ans (average), regardless of whether A1-ans is less than 0, negative numbers equal to other places to the number, can be quite it will be negative Number to other places, because the number of moves is the same, so do not distinguish between negative numbers can not move, and then the same A2->a3 a3->a4 ..., can be seen as only the right to move the case, and then calculate the counter is the minimum number of moves. The solution as a whole is to ask for the most number of intervals and for ans.
#include <iostream>#include<algorithm>using namespacestd;inta[ the];intMain () {intn,counter,sum; scanf ("%d",&N); Counter= SUM =0; for(intI=0; i<n;i++) {scanf ("%d",&A[i]); Sum+=A[i]; } Sum/=N; for(intI=0; i<n;i++){ if(a[i]!=sum) {A[i+1] + = A[i]-sum; Counter++; }} printf ("%d\n", counter); return 0;}
Split Solitaire (the maximum number of greedy intervals and averages)