Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- using namespace std;
- using namespace __gnu_pbds;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef tree<int,null_type,less<>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
- #define all(v) v.begin(),v.end()
- #define rall(v) v.rbegin(),v.rend()
- #define MOD 1000000007
- #define PI 3.14159265
- //#define ceil(a, b) ((a / b) + (a % b ? 1 : 0))
- #define imin INT_MIN
- #define imax INT_MAX
- #define nl '\n'
- #define modulo(a, b, m) ((a % m) * (b % m)) % m
- void Start_Crushing() {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- }
- //vector<int> dx = {0, 0, 1, -1, 1, 1, -1, -1}, dy = {1, -1, 0, 0, 1, -1, 1, -1};
- //int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
- void solve(){
- int n; cin >> n;
- map<int, int> rem;
- for(int i = 0; i < n; i++){
- int x; cin >> x;
- rem[x % 3]++;
- }
- ll sum = 0;
- pair<int, int> maxi = {imin, imin};
- for(auto& [x, y] : rem){
- if(y > maxi.second) maxi = {x, y};
- }
- // while(min + 1 < max)
- // 5 4
- int diff = rem[maxi.first] - rem[(maxi.first + 1) % 3];
- sum += diff;
- rem[maxi.first] -= diff, rem[(maxi.first + 1) % 3] -= diff;
- cout << sum;
- }
- int main(){
- //freopen("equal.in", "r", stdin);
- Start_Crushing();
- int t = 1;
- /*is Single Test case?*/ cin >> t;
- while (t--) {
- solve();
- if(!t) break;
- cout << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement