fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define e '\n'
  4. #define ll long long
  5. #define maxn 1000006
  6. int a[maxn];
  7. int b[maxn];
  8. int ab[maxn];
  9. ll c;
  10. string s;
  11. int n;
  12.  
  13. bool check(int x){
  14. for(int i = 1;i+x-1<=n;i++){
  15. int k = i + x - 1;
  16. int cnt = ab[k] - b[k] * a[i-1];
  17. if(cnt <= c) return 1;
  18. }
  19. return 0;
  20. }
  21.  
  22. int main(){
  23. ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  24. cin >> n >> c >> s;
  25. s = ' ' + s;
  26. for(int i = 1;i<=n;i++){
  27. if(s[i] == 'a'){
  28. a[i]++;
  29. }
  30. if(s[i] == 'b') {
  31. b[i]++;
  32. ab[i] += b[i] * a[i-1];
  33. }
  34. a[i] += a[i-1];
  35. b[i] += b[i-1];
  36. ab[i] += ab[i-1];
  37. }
  38. int l = 1;
  39. int r = n;
  40. int res = 0;
  41. while(l <= r){
  42. int mid = (l + r) / 2;
  43. if(check(mid)){
  44. res = mid;
  45. l = mid + 1;
  46. }
  47. else r = mid - 1;
  48. }
  49. cout << res;
  50. }
  51.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty