"Greedy" "Codevs" 1098 split Solitaire

Source: Internet
Author: User

http://codevs.cn/problem/1098/

Q:n stacks of cards, each stack of cards randomly, each pile of cards can only move to the adjacent card stack, the minimum number of moves, so that the number of cards per stack consistent.

A: Sweep from beginning to end, as long as the current stack of cards is not the average number of cards per stack, it will be more or less of the cards moved or taken from the next pile of cards, while the number of moves plus 1

#include <bits/stdc++.h>using namespacestd;intp[ the];intN;intsum, avg;intans;intMain () { while(~SCANF ("%d", &N)) {Sum=0; Ans=0;  for(inti =0; I < n; i++) {scanf ("%d", &P[i]); Sum+=P[i]; } avg= SUM/N;  for(inti =0; I < n; i++){            if(P[i] <avg) {P[i+1]-= avg-P[i]; P[i]=avg; Ans++; }            Else if(P[i] >avg) {P[i+1] + = p[i]-avg; P[i]=avg; Ans++; }} printf ("%d\n", ans); }    return 0;}

"Greedy" "Codevs" 1098 split Solitaire

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.