#include <bits/stdc++.h> using namespace std; typedef long long LL; int a[8009]; int b[8009]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, k, t; string s; cin>>n>>k>>t>>s; int res = -1; for(int i=1; i<=n; i++){ a[i] += a[i-1]; b[i] += b[i-1]; if(s[i-1]=='1'){ a[i]++; }else if(s[i-1]=='2'){ b[i]++; } } if(a[n] <= k){ int x = max(0, b[n] - (k-a[n])); res = n - x; } for(int i=1; i<=n-t-t+1; i++){ for(int j=i+t; j<=n-t+1; j++){ int y = (a[i+t-1] - a[i-1]) + (b[i+t-1] - b[i-1]) + (a[j+t-1] - a[j-1]) + (b[j+t-1] - b[j-1]); y += a[i-1] + (a[n] - a[j+t-1]); if(y <= k){ int x = b[i-1] + (b[n] - b[j+t-1]); x = max(0, x - (k-y)); res = max(res, n-x - (j+t-i)); } } } cout<<res<<"\n"; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include <bits/stdc++.h> using namespace std; typedef long long LL; int a[8009]; int b[8009]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, k, t; string s; cin>>n>>k>>t>>s; int res = -1; for(int i=1; i<=n; i++){ a[i] += a[i-1]; b[i] += b[i-1]; if(s[i-1]=='1'){ a[i]++; }else if(s[i-1]=='2'){ b[i]++; } } if(a[n] <= k){ int x = max(0, b[n] - (k-a[n])); res = n - x; } for(int i=1; i<=n-t-t+1; i++){ for(int j=i+t; j<=n-t+1; j++){ int y = (a[i+t-1] - a[i-1]) + (b[i+t-1] - b[i-1]) + (a[j+t-1] - a[j-1]) + (b[j+t-1] - b[j-1]); y += a[i-1] + (a[n] - a[j+t-1]); if(y <= k){ int x = b[i-1] + (b[n] - b[j+t-1]); x = max(0, x - (k-y)); res = max(res, n-x - (j+t-i)); } } } cout<<res<<"\n"; return 0; } |