1320: sharing card (Noip2002)

 

Description [title]

N-card stack, are numbered 1,2, ..., n. There are a number of sheets per stack, but the total number of cards will be for a multiple of n. A plurality of cards can take on either a pile and then moved.

Card moving rules: Take numbered card stack 1, stack 2 can only move a number; the number n of cards taken heap, the heap can only be moved to the number n-1, ; others take the heap of cards, can be moved left or right adjacent to the heap.

Now asked to find a mobile method with the least number of moves are the number of cards per heap as much.

N = 4,4, for example, the number of the card stack are: ① 9 ② 8 ③ 17 ④ 6

3 can achieve the purpose of movement:

4 taken from the card into ③ ④ (9 8 13 10) -> 3 taken from the card into ③ ② (9 11 10 10) -> taken from a card placed ② ① (10 10 10 10).

[Enter]

n (n stack of cards, 1 ≤ n ≤ 100)

a1 a2 ... an (n stack of cards, each card number of the initial stack, l≤ ai ≤10000).

[Output]

All stacks have reached the minimum number of moves equal.

[Sample input]

4
9 8 17 6

[Sample Output]

3

 Note asterisk place, there must be left to determine <n and the like, would have been otherwise left increment until the array bounds, resulting in operational errors

// Created on 2020/2/11

/*#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <climits>*/
#include <bits/stdc++.h>

using namespace std;

const int idata=1000+5;
int goal[idata];
int n,m;
int ACanswer;

int main()
{
    int sum=0,average=0;
    int i,j;
    cin>>n;

    for(i=1;i<=n;i++)
    {
        cin>>goal[i];
        sum+=goal[i];
    }

    average=sum/n;
    for(i=1;i<=n;i++)
        goal[i]-=average;

    int left=1,right=n;
    while(goal[left]==0&&left<n) left++;     //*
    while(goal[right]==0&&right>1) right--;  //*

    while(left<right)
    {
        goal[left+1]+=goal[left];
        goal[left]=0;
        ACanswer++;
        left++;
        while(goal[left]==0&&left<right) left++;//*
    }

    cout<<ACanswer<<endl;

}

 

Published 177 original articles · won praise 8 · views 6339

Guess you like

Origin blog.csdn.net/C_Dreamy/article/details/104266597