/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 324.0 KiB
#2 Accepted 1ms 444.0 KiB
#3 Accepted 45ms 568.0 KiB
#4 Accepted 70ms 2.562 MiB
#5 Accepted 127ms 6.355 MiB
#6 Accepted 137ms 6.344 MiB
#7 Accepted 102ms 656.0 KiB
#8 Accepted 99ms 1.047 MiB
#9 Accepted 43ms 2.84 MiB
#10 Accepted 53ms 616.0 KiB
#11 Accepted 35ms 1.316 MiB

Code

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1000000007;
#define sz(x) (ll)(x).size()
#define rd ({ll x; cin >> x; x; })
#define dbg(x) cerr << "[" #x "]  " << (x) << "\n\n"
// #define errv(x) {cerr << "["#x"]  ["; for (const auto& ___ : (x)) cerr << ___ << ", "; cerr << "]\n";}
// #define errvn(x, n) {cerr << "["#x"]  ["; for (auto ___ = 0; ___ < (n); ++___) cerr << (x)[___] << ", "; cerr << "]\n";}
// #define cerr if(0)cerr
#define xx first
#define yy second
mt19937 rnd(std::chrono::high_resolution_clock::now().time_since_epoch().count());
/*_________________________________________________________________________________________________________________________*/

void Solve()
{
    ll n, k;
    cin >> n >> k;
    vector<ll> arr(n);
    map<ll, ll> mp;
    for (int i = 0; i < n; i++)
        cin >> arr[i], mp[arr[i]]++;
    multiset<ll> pq;
    for (auto& it : mp)
        pq.insert(it.yy);
    vector<ll> ans(k, n / k);
    ll mod1 = n % k;
    for (int i = 0; i < mod1; i++)
        ans[i]++;
    ll c = 0;
    for (auto& it : ans) {
        auto base = *pq.rbegin();
        pq.erase(pq.find(base));
        if (base >= it) {
            base -= it;
            if (base)
                pq.insert(base);
        } else {
            ll gap = it - base;
            c += gap;
            while (gap) {
                auto small = *pq.begin();
                pq.erase(pq.find(small));
                if (small >= gap) {
                    small -= gap;
                    if (small)
                        pq.insert(small);
                    gap = 0;
                } else {
                    gap -= small;
                }
            }
        }
    }
    cout << c << "\n";
}

int32_t main()
{
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t = 1;
    cin >> t;
    for (int i = 1; i <= t; i++) {
        // cout << "Case #" << i << ": "; // cout << "Case " << i << ": ";
        Solve();
    }
    return 0;
}
// Coded by Tahsin Arafat (@TahsinArafat)

Information

Submit By
Type
Submission
Problem
P1062 Roy and Array
Contest
Brain Booster #4
Language
C++20 (G++ 13.2.0)
Submit At
2024-07-14 18:09:59
Judged At
2024-10-03 13:35:21
Judged By
Score
100
Total Time
137ms
Peak Memory
6.355 MiB