/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 332.0 KiB
#2 Accepted 2ms 328.0 KiB
#3 Accepted 89ms 576.0 KiB
#4 Accepted 94ms 2.402 MiB
#5 Accepted 128ms 4.949 MiB
#6 Accepted 126ms 4.703 MiB
#7 Accepted 136ms 584.0 KiB
#8 Accepted 144ms 1.016 MiB
#9 Accepted 58ms 2.547 MiB
#10 Accepted 90ms 572.0 KiB
#11 Accepted 51ms 1.043 MiB

Code

#include <bits/stdc++.h>
#define ll long long
#define endll '\n';
#define pb push_back
#define all(v) v.begin(), v.end()
using namespace std;

const int mod = 1e9 + 7, N = 1e6;

void solve()
{
    int n, k;
    cin >> n >> k;
    int ar[n + 3];
    int need[k + 3];
    map<int, int> mp;
    multiset<int, greater<>> st;
    for (int i = 1; i <= n; i++)
        cin >> ar[i], mp[ar[i]]++;
    for (auto i : mp)
    {
        st.insert(i.second);
    }

    // sort(all(v), greater<>());
    int ps = 1;
    for (int i = 1; i <= n; i++)
    {
        if (i <= k)
        {
            need[i] = 1;
        }
        else
        {
            need[ps] += 1;
            ps++;
            if (ps > k)
                ps = 1;
        }
    }

    vector<int> v;
    for (int i = 1; i <= k; i++)
    {
        v.pb(need[i]);
    }

    sort(all(v), greater<>());

int ans = 0;
    for (int i = 0; i < k; i++)
    {

        // cout << v[i] << " = need" << endll;
        int x = *st.begin();
        st.erase(st.begin());
        if(v[i] <=    x) {
            x -= v[i];
            st.insert(x);
        }
        else {
            ans += v[i] - x;
            
        }

    }
    cout << ans << endl;
    // cout << endll;
    // cout << endll;
}

int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

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 17:51:31
Judged At
2024-10-03 13:35:47
Judged By
Score
100
Total Time
144ms
Peak Memory
4.949 MiB