Codeforces-1490B Balanced Remainders


Problem

Portal


Meaning

给三个数,求每次选择一个数-1,他的后一个数+1(第三个数的后一个数是第一个数),问最少需要多少步是的三个数相等。


Thoughts

每次选最大的数-1。


Accepted Code

#include <bits/stdc++.h>
#define endl '\n'
#define int long long

using namespace std;

const int N = 1e6+10;

int a[N], c[3];

bool check()
{
    for(int i = 1 ; i <= 2 ; i ++ ) 
        if(c[i] != c[0]) return true;
    return false;
}

signed main()
{
#ifdef w
    freopen("out", "w", stdout);
#endif
    int T; cin >> T;
    while(T -- )
    {
        c[0] = c[1] = c[2] = 0;
        int n, ans = 0; cin >> n;
        for(int i = 1 ; i <= n ; i ++ ) 
        {
            cin >> a[i];
            c[a[i]%3] ++ ;
        }
        while(check())
        {
            int sub = max_element(c, c+3)-c, add = (max_element(c, c+3)-c+1)%3;
            c[sub] -- , c[add] ++ ;
            ans ++ ;
        }
        cout << ans << endl;
    }
    return 0;
}

Author: Mrhh
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source Mrhh !
评论
 Previous
Codeforces-1486A Shifting Stacks Codeforces-1486A Shifting Stacks
ProblemPortal Meaning给定一个序列,每次将第i个数-1,第i+1个数+1,问是否能够将其变成严格单调递增的序列。 Thoughts输出no的情况,当前数不够大,所以在前面我们尽可能的将当前数做到条件允许的最小值(0,
Next 
  TOC