Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ll long long
- using namespace std;
- const int N = 1e6 + 5;
- ll n, c;
- string s;
- int main() {
- freopen("MONOTONE.inp", "r", stdin);
- freopen("MONOTONE.out", "w", stdout);
- ios_base::sync_with_stdio(false);
- cin.tie(NULL); cout.tie(NULL);
- cin >> n >> c >> s;
- ll inca = 0, incb = 0, ab = 0;
- ll ans = 0;
- ll j = 0;
- for(ll i = 0; i < n; ++i) {
- while(j < n && ab <= c) {
- ans = max(ans, (j - i) * 1LL);
- if(s[j] == 'a')
- ++inca;
- else if(s[j] == 'b')
- ++incb, ab += inca;
- ++j;
- }
- if(ab <= c) ans = max(ans, (j - i) * 1LL);
- if(s[i] == 'b')
- --incb;
- else if(s[i] == 'a')
- --inca, ab -= incb;
- }
- cout << ans << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment