Question B: "Greedy" sharing cards Title Description
There were N piles of cards, numbered 1,2,...,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, 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 cards and put them in ① (10 10 10 10).
Input
The input file includes two rows of data. The first action N heap of Cards (1<=n<=100). the second behavior N heap solitaire in each pile of solitaire initial number A1,a2,...,an (l<=ai<=10000).
Output
The only line in the output file, which is the minimum number of moves when all the heaps are equal.
Sample input
49 8 17 6
Sample output
3
Minus the average, you get a string of positive and negative numbers, and if it's positive, it's redundant, if it's negative, it's not enough, if it's zero, it doesn't mean much.
#include <iostream>using namespace Std;int a[10001];int main () {int n;while (cin >> N) {int avg = 0;int count = 0;for (int i = 0;i < n;i++) {cin >> A[i];avg + = A[i];} AVG = avg/n;for (int i = 0;i < n;i++) {A[i]-= AVG;IF (a[i]! = 0) {count++; A[i + 1] + = A[i];}} cout << count << Endl;} return 0;}
Greedy practice (a) Sharing cards