NOIP2002 [improve group] card sharing solution to a problem

Face questions

That topic guarantee the solvability total number of cards that can be divisible (N | T) each person holds a card a [1] ... a [m], we can consider the first man

1. If a [1]> T / M, the second person to the first person need c [1] -T / M of cards, i.e., the c [2] plus c [1] -T / M.

2. If a [1] <T / M, the first person needs to take a second person c [1] -T / M of cards, i.e., the c [2] by subtracting T / Mc [1].

We can turn to consider 2 ~ M individual in accordance with this method. Even if a certain time has c [i] is reduced to a negative number does not matter, because then c [i] will [i + 1] to take the card from the c.

Code

#include<bits/stdc++.h>
using namespace std;
int m,a[105],T,ans;
int main(){
    scanf("%d",&m);
    for(int i=1;i<=m;++i){
        scanf("%d",&a[i]);
        T+=a[i];
    }
    T/=m;
    for(int i=1;i<=m;++i){
        a[i]-=T;
    }
    for(int i=1;i<=m;++i){
        if(a[i]!=0){
            a[i+1]+=a[i];
            ans++;
        }
    }
    printf("%d",ans);
    return 0;
}

On this issue can also be a development, if you can only take one card, the idea is the same with the above, the minimum number of steps is

$ \ Sum_ {i = 1} ^ M $ $ \ mid $ i * T / MG [i] $ \ mid $, wherein G is a prefix and that G [i] = $ \ sum_ {j = 1} ^ i $ a [i]

Wherein the meaning of each of the "prefix" initially G [i] playing cards, and finally there i * T / M playing cards.

If we set A [i] = a [i] -T / M, ie the outset the number of cards in the hands of each person are minus T / M, and eventually the hands of every human being has 0 card, the answer is still unchanged, that is,

$ \ Sum_ {i = 1} ^ M $ $ \ mid $ S [i] $ \ mid $ where S is the prefix and i.e., S A a [i] = $ \ sum_ {j = 1} ^ i $ A [i ]

Guess you like

Origin www.cnblogs.com/donkey2603089141/p/11414580.html