Code vs 1098 Sharing Solitaire (greedy)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.