Advertisement
pb_jiang

CF1619E

Mar 23rd, 2025
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. // Problem: E. MEX and Increments
  2. // Contest: Codeforces - Codeforces Round 762 (Div. 3)
  3. // URL: https://codeforces.com/problemset/problem/1619/E
  4. // Memory Limit: 256 MB
  5. // Time Limit: 2000 ms
  6. //
  7. // Powered by CP Editor (https://cpeditor.org)
  8.  
  9. #include <assert.h>
  10. #include <bits/stdc++.h>
  11. using namespace std;
  12. #ifndef __DEBUG__
  13. #define dbg(...) 42
  14. #endif
  15. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  16.  
  17. using ll = long long;
  18. using a2l = array<ll, 2>;
  19. using pll = pair<ll, ll>;
  20. using vl = vector<ll>;
  21.  
  22. void solve()
  23. {
  24.     ll n, mex = 0;
  25.     cin >> n;
  26.     vl a(n), cnt(n + 1);
  27.     for (auto &x : a) {
  28.         cin >> x;
  29.         if (x <= n)
  30.             cnt[x] += 1;
  31.     }
  32.     while (mex <= n && cnt[mex])
  33.         ++mex;
  34.  
  35.     vl ans(n + 1, -1);
  36.     map<ll, ll> opt;
  37.     for (ll i = 0; i < mex; ++i) {
  38.         ans[i] = cnt[i];
  39.         if (cnt[i] > 1)
  40.             opt[i] = cnt[i] - 1;
  41.     }
  42.     ans[mex] = 0;
  43.     for (ll i = mex + 1, acc = 0; i <= n; ++i) {
  44.         dbg(opt);
  45.         if (opt.size() == 0) {
  46.             break;
  47.         }
  48.         auto [k, v] = *opt.rbegin();
  49.         acc += i - 1 - k;
  50.         if (v == 1)
  51.             opt.erase(k);
  52.         else
  53.             opt[k] -= 1;
  54.         ans[i] = acc + cnt[i];
  55.         if (cnt[i])
  56.             opt[i] = cnt[i];
  57.     }
  58.     for (auto x : ans)
  59.         cout << x << ' ';
  60.     cout << '\n';
  61. }
  62.  
  63. int main(int argc, char **argv)
  64. {
  65.     ll t;
  66.     cin >> t;
  67.     while (t--)
  68.         solve();
  69.     return 0;
  70. };
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement