题目链接
https://codeforces.com/contest/1490/status/B
题解
分类:
思维
思路:
统计个数,加加减减就好了,但是记住写好条件
我的代码:
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 3e4 + 10;
int a[maxn];
int main(){
int t, n, i, k[3];cin >> t;while (t--){
int sum = 0;for (i = 0; i < 3; i++)k[i] = 0;cin >> n;for (i = 0; i < n; i++){
cin >> a[i];int temp = a[i] % 3;k[temp]++;}while (1){
if (k[0] > (n / 3)){
sum += k[0] - (n / 3);k[1] += k[0] - (n / 3);k[0] = (n / 3);}if (k[1] > (n / 3)){
sum += k[1] - (n / 3);k[2] += k[1] - (n / 3);k[1] = (n / 3);}if (k[2] > (n / 3)){
sum += k[2] - (n / 3);k[0] += k[2] - (n / 3);k[2] = (n / 3);}if (k[0] == k[1] && k[1] == k[2])break;}cout << sum << "\n";}return 0;
}