题目大意:给定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;
}