1098 Sharing Cards 2002 NOIP National League Improvement Grouptime limit: 1 sspace limit: 128000 KBtitle level: Golden Gold Solving Title Description
Description
now requires finding a way to move, Use the fewest number of moves to make as many cards as you can on each heap.
For example n=4,4 heap the number of cards is:
①9②8③17④6
move 3 times to achieve the goal:
take 4 cards from ③ and put them in ④ (9 8 13 From ③ take 3 cards to ② (9), from ② take 1 cards to ① (10 10 10 10).
Enter a description input
Description
First row n (n heap Solitaire, 1 <= n <= 100)
Second line A1 A2 ... An (N heap solitaire, initial number of cards per pile, l<= Ai <=10000)
outputs description output
Description
Output to the screen. The format is:
The minimum number of moves when all heaps are equal. ‘
sample input to
sample
4
9 8 17 6
Sample output Sample
outputs
3
data
size & Hint
E
Category Tags tags
click here to expand
Idea: Greedy from left to right traversal, if the number of traversed to exceed the average, the number to the average, the next number + out of the number, if not the average, from the next number, the next digit-(average-the number of);
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;intn,a[10000],sum,pj,ans;intMain () {CIN>>N; for(intI=1; i<=n;i++) {cin>>A[i]; Sum+=A[i]; } PJ=sum/N; for(intI=1; i<=n;i++){ if(a[i]<PJ) {A[i+1]=a[i+1]-(pj-A[i]); A[i]=PJ; Ans++; } if(a[i]>PJ) {A[i+1]=a[i+1]+ (a[i]-PJ); A[i]=PJ; Ans++; }} cout<<ans;}
If you have some help, don't forget to praise oh; See you next time! 88
Code vs 1098 Sharing Solitaire (greedy)