noip2002 sharing Solitaire (analog)

Source: Internet
Author: User
P1123 Split Solitaire Accepted Tags: greedy noip raise Group 2002 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, the n=4,4 heap of cards are:
①9②8③17④6
Move 3 times to achieve the goal:
Take 4 cards from ③ to ④ (9 8) and take 3 cards from ③ to ② (9), take 1 cards from ② and put them in ① (10 10 10 10). format Input Format

N (n heap Solitaire, 1 <= n <= 100)
A1 A2 ... An (N heap solitaire, initial number of cards per pile, l<= Ai <=10000) output format

The minimum number of moves when all heaps are equal. Example 1 sample input 1[copy]

4
9 8) 17 6
sample Output 1[copy]
3
Limit

1s source per test point

The first problem of NOIP2002 raising group


Code:

#include <cstdio>
using namespace std;

const int maxn=100;
int n,a[maxn+50];

int main ()
{
  int i,j,k,ans=0;
  scanf ("%d", &n);
  for (k=0,i=1;i<=n;i++)
    scanf ("%d", &a[i]), k+=a[i];
  for (j=k/n,i=1;i<=n;i++) a[i]-=j;
  for (i=1;i<n;i++)
    if (a[i]!=0) a[i+1]+=a[i],ans++;
  printf ("%d\n", ans);
  return 0;
}


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.