cf补题:
https://codeforces.com/contest/1490/problem/B
审题不清,必须保证每一步都是increasing
官方题解:
https://codeforces.com/blog/entry/87874
ac代码
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
#define IO std::ios::sync_with_stdio(false); cin.tie(0)
#define ll long long
#define ull unsigned long long
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define per(i, l, r) for (int i = l; i >= r; --i)
#define mset(s, _) memset(s, _, sizeof(s))
#define mcpy(s, _) memcpy(s, _, sizeof(s))
#define pb push_back
#define pii pair <int, int>
#define vi vector<int>
#define vpii vector<pii>
#define mp(a, b) make_pair(a, b)
#define debug system("pause")
#define pll pair <ll, ll>
#define fir first
#define sec second
#define inf 0x3f3f3f3f
#define pline cout << endl << endl
inline int lowbit(int x) {return x & -x;}
inline bool cmp(int a, int b) {return a > b;}
template< typename T > inline void get_min(T &x, T y) {if(y < x) x = y;}
template< typename T > inline void get_max(T &x, T y) {if(x < y) x = y;}
inline int read() {
int x = 0, f = 0; char ch = getchar();
while (!isdigit(ch)) f |= ch == '-', ch = getchar();
while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar();
return f ? -x : x;
}
template<typename T> inline void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) print(x / 10);
putchar(x % 10 + '0');
}
template<typename T> inline void print(T x, char let) {
print(x), putchar(let);
}
const int N = 3e4 + 10;
int t, n;
int a[N];
int num[N];
int cnt;
int ans;
map<int, int> mp;
int main(){
IO;
cin >> t;
while(t -- ) {
cnt = 0, ans = 0;
mset(num, 0);
cin >> n;
rep(i, 0, n - 1) {
cin >> a[i];
int t = a[i] % 3;
num[t] ++ ;
// cout << a[i] << " " << t << endl;
}
// rep(i, 0, 2) cout << i << " " << num[i] << endl;
int x = n / 3;
int id;
rep(i, 0, 2) {
if(num[i] < x) cnt ++ , id = i;
}
if(cnt == 1) {
rep(i, 0, 2) {
if(i != id) {
if(i < id) ans += abs(i - id) * abs(num[i] - x);
else ans += abs(3 - i + id) * abs(num[i] - x);
// cout << "***" << endl;
}
}
cout << ans << endl;
continue;
}
ans = 0;
cnt = 0;
id = 0;
rep(i, 0, 2) {
if(num[i] > x) cnt ++ , id = i;
}
if(cnt == 1) {
rep(i, 0, 2) {
if(i != id) {
if(i > id) ans += abs(i - id) * abs(x - num[i]);
else ans += abs(3 - id + i) * abs(x - num[i]);
}
// cout << "!!!!" << endl;
}
cout << ans << endl;
continue;
}
cout << 0 << endl;
}
return 0;
}