Advertisement
pb_jiang

CF786C

May 19th, 2023
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. // Problem: C. Till I Collapse
  2. // Contest: Codeforces - Codeforces Round 406 (Div. 1)
  3. // URL: https://codeforces.com/contest/786/problem/C
  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.  
  13. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  14.  
  15. using ll = long long;
  16. using pii = pair<int, int>;
  17. using pll = pair<ll, ll>;
  18. using vl = vector<ll>;
  19. using vi = vector<int>;
  20.  
  21. int calc(const vi &v, int k)
  22. {
  23.     int ret = 0;
  24.     set<int> sz;
  25.     for (auto x : v) {
  26.         if (sz.size() == k && sz.count(x) == 0) {
  27.             sz.clear();
  28.             ++ret;
  29.         }
  30.         sz.insert(x);
  31.     }
  32.     if (sz.size())
  33.         ++ret;
  34.     return ret;
  35. }
  36.  
  37. int calc1(const vi &v, int k)
  38. {
  39.     vi time(1e5 + 3);
  40.     int clock = 1, left = k, ret = 0;
  41.     for (auto x : v) {
  42.         if (time[x] == clock)
  43.             continue;
  44.         if (left == 0) {
  45.             left = k;
  46.             ++clock;
  47.             ++ret;
  48.         }
  49.         time[x] = clock;
  50.         --left;
  51.     }
  52.     return ret + (left != k);
  53. }
  54.  
  55. void search(vi &ans, const vi &v, int l, int r)
  56. {
  57.     if (ans[l] == -1)
  58.         ans[l] = calc1(v, l + 1);
  59.     if (ans[r - 1] == -1)
  60.         ans[r - 1] = calc1(v, r);
  61.     if (ans[l] == ans[r - 1]) {
  62.         for (int i = l + 1; i < r - 1; ++i)
  63.             ans[i] = ans[l];
  64.         return;
  65.     }
  66.     if (l + 1 == r)
  67.         return;
  68.     int m = (l + r) / 2;
  69.     search(ans, v, l, m), search(ans, v, m, r);
  70. }
  71.  
  72. int main(int argc, char **argv)
  73. {
  74.     int n;
  75.     cin >> n;
  76.     vi a(n);
  77.     for (auto &x : a)
  78.         cin >> x;
  79.     a.erase(std::unique(a.begin(), a.end()), a.end());
  80.     vi ans(n, -1);
  81.  
  82.     search(ans, a, 0, n);
  83.     for (auto x : ans)
  84.         cout << x << ' ';
  85.     cout << endl;
  86.     return 0;
  87. };
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement