Advertisement
pb_jiang

CF190D

Jun 2nd, 2023
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. // Problem: D. Non-Secret Cypher
  2. // Contest: Codeforces - Codeforces Round 120 (Div. 2)
  3. // URL: https://codeforces.com/problemset/problem/190/D
  4. // Memory Limit: 256 MB
  5. // Time Limit: 3000 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. #ifdef __DEBUG__
  13. #include "dbg.h"
  14. #else
  15. #define dbg(...) 42
  16. #endif
  17. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  18.  
  19. using ll = long long;
  20. using pii = pair<int, int>;
  21. using pll = pair<ll, ll>;
  22. using vl = vector<ll>;
  23. using vi = vector<int>;
  24.  
  25. int main0(int argc, char **argv)
  26. {
  27.     int n, k;
  28.     cin >> n >> k;
  29.     vi a(n);
  30.     for (auto &x : a)
  31.         cin >> x;
  32.     ll ans = 0;
  33.     map<int, int> cnt;
  34.     int last_hit = -1;
  35.     bool satisfy = false;
  36.     for (int i = 0, j = 0; i < n; ++i) {
  37.         while (j < n && !satisfy) {
  38.             cnt[a[j]]++;
  39.             if (cnt[a[j]] >= k)
  40.                 satisfy = true, last_hit = a[j];
  41.             ++j;
  42.         }
  43.         dbg(i, j, last_hit, n - j + 1);
  44.         ans += (last_hit == -1 ? 0 : n - j + 1);
  45.         if (a[i] == last_hit)
  46.             satisfy = false, last_hit = -1;
  47.         cnt[a[i]]--;
  48.     }
  49.     cout << ans << endl;
  50.     return 0;
  51. };
  52.  
  53. int main(int argc, char **argv)
  54. {
  55.     int n, k;
  56.     cin >> n >> k;
  57.     vi a(n);
  58.     for (auto &x : a)
  59.         cin >> x;
  60.     map<int, int> cnt;
  61.     ll ans = 0;
  62.     int left = 0;
  63.     for (auto x : a) {
  64.         cnt[x]++;
  65.         while (cnt[x] >= k) {
  66.             cnt[a[left]]--;
  67.             ++left;
  68.         }
  69.         ans += left;
  70.     }
  71.     cout << ans << endl;
  72.     return 0;
  73. };
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement