B. Balanced Remainders

传送门

在这里插入图片描述

题目大意:给定n个数,每个数%3的remainder为别是0、1、2,分别计为a、b、c。每个数可以进行一次或者是多次的加一操作,问:经过多少次操作可以使得a、b、c相等?
题解:(1)remainder不是0就是1和2,通过改变加一相应减一的操作就可以实现相等,但是这种循环是正向的,不然次数要减少!具体见代码。

#include <bits/stdc++.h>
#define ios ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
using namespace std;
const int max_n = 3e4;
int a[max_n+5], b[3];

bool equle(int a, int b, int c)
{
    if(a == b && a == c && b == c) return true;
    else return false;
}

int main()
{
    ios;
    
    int t;
    cin >> t;
    while( t-- )
    {
        int n;
        cin >> n ;
        
        int c_0 = 0;
        int c_1 = 0;
        int c_2 = 0;
        for (int i = 1;i <= n; i++) 
        {
            cin >> a[i];
            if (a[i] % 3 == 0) b[0]++;
            else if (a[i] % 3 == 1) b[1]++;
            else b[2]++;
        }
        
        if ( equle(b[0], b[1], b[2]) ) 
        	cout << "0" << endl;
        else
        {
            int cnt = 0;
            while( !(equle(b[0], b[1], b[2])) )
            {
                if(b[0] > b[1]) {
                    b[0]--;
                    b[1]++;
                    cnt++;
                }
                if(b[1] > b[2]){
                    b[1]--;
                    b[2]++;
                    cnt++;
                }
                if(b[2] > b[0]){
                    b[2]--;
                    b[0]++;
                    cnt++;
                }
            }
            cout << cnt << endl;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值