Advertisement
Tbl_Mne_Ne_Dryg

Untitled

Dec 25th, 2022
1,073
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int maxn = 5 * 1e6 + 11;
  6.  
  7. struct myset {
  8.   vector<int> cnt;
  9.   int mx = 0;
  10.   myset() {
  11.     cnt.resize(maxn);
  12.   }
  13.   int get_max() {
  14.     return mx;
  15.   }
  16.   void decrease(int x) {
  17.     if (x == mx && cnt[x] == 1)
  18.       mx -= 1;
  19.     if (x >= 0)
  20.       cnt[x]--;
  21.     if (x > 0)
  22.       cnt[x - 1]++;
  23.   }
  24.   void increase(int x) {
  25.     if (x == mx)
  26.       mx += 1;
  27.     if (x >= 0)
  28.       cnt[x]--;
  29.     cnt[x + 1]++;
  30.   }
  31. };
  32.  
  33. signed main() {
  34.   ios::sync_with_stdio(false);
  35.   cin.tie(0);
  36. #ifdef LOCAL
  37.   freopen("input", "r", stdin);
  38. #endif
  39.   int n, k;
  40.   cin >> n >> k;
  41.   string s;
  42.   cin >> s;
  43.   int ans = 0;
  44.   int r = 0;
  45.   int cnt_ch['z' + 1], cnt_nch['z' + 1];
  46.   memset(cnt_ch, 0, sizeof cnt_ch), memset(cnt_nch, 0, sizeof cnt_nch);
  47.   myset ch, nch;
  48.   ch.mx = 1;
  49.   ch.cnt[1] = 1;
  50.   cnt_ch[s[0]] += 1;
  51.   auto calc = [&](int l, int r) {
  52.     return (r - l + 1) - ch.get_max() - nch.get_max();
  53.   };
  54.   for (int l = 0; l < n; l++) {
  55.     while (calc(l, r) <= k) {
  56.       r += 1;
  57.       if (r == n)
  58.         break;
  59.       if (r % 2) {
  60.         nch.increase(cnt_nch[s[r]]++);
  61.       } else {
  62.         ch.increase(cnt_ch[s[r]]++);
  63.       }
  64.     }
  65.     if (r % 2) {
  66.       nch.decrease(cnt_nch[s[r]]--);
  67.     } else {
  68.       ch.decrease(cnt_ch[s[r]]--);
  69.     }
  70.     r -= 1;
  71.     ans = max(ans, r - l + 1);
  72.     if (l % 2) {
  73.       nch.decrease(cnt_nch[s[l]]--);
  74.     } else {
  75.       ch.decrease(cnt_ch[s[l]]--);
  76.     }
  77.   }
  78.   cout << ans << '\n';
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement