#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
cout.tie(NULL);
int n,k,t;
cin>>n>>k>>t;
string s;
cin>>s;
vector<int>z(n+1,0),b(n+1,0),w(n+1,0);
for(int i=0; i<n; i++)
{
z[i+1] = z[i];
b[i+1] = b[i];
w[i+1] = w[i];
if(s[i]=='1')
b[i+1]++;
if(s[i]=='2')
z[i+1]++;
if(s[i]=='3')
w[i+1]++;
}
int res=-1;
if(b[n]<=k)
res=w[n]+b[n]+min(k-b[n],z[n]);
else
for(int i=t+1; i<=n; i++)
for(int j=n-t; j>=i; j--)
{
int res1=w[i-t-1]+w[n]-w[j+t];
if(n-j+i-1-2*t<res)
continue;
int im=b[i-t-1]+b[n]-b[j+t];
if(im>k)
break;
im=im+b[i-1]-b[i-t-1]+b[j+t]-b[j];
im=im+z[i-1]-z[i-t-1]+z[j+t]-z[j];
if(im>k)
continue;
res1+=min(k-im,z[i-t-1]+z[n]-z[j+t]);
res1=res1+b[i-t-1]+b[n]-b[j+t];
res=max(res,res1);
}
cout<<res<<"\n";
}
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; int main() { ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL); int n,k,t; cin>>n>>k>>t; string s; cin>>s; vector<int>z(n+1,0),b(n+1,0),w(n+1,0); for(int i=0; i<n; i++) { z[i+1] = z[i]; b[i+1] = b[i]; w[i+1] = w[i]; if(s[i]=='1') b[i+1]++; if(s[i]=='2') z[i+1]++; if(s[i]=='3') w[i+1]++; } int res=-1; if(b[n]<=k) res=w[n]+b[n]+min(k-b[n],z[n]); else for(int i=t+1; i<=n; i++) for(int j=n-t; j>=i; j--) { int res1=w[i-t-1]+w[n]-w[j+t]; if(n-j+i-1-2*t<res) continue; int im=b[i-t-1]+b[n]-b[j+t]; if(im>k) break; im=im+b[i-1]-b[i-t-1]+b[j+t]-b[j]; im=im+z[i-1]-z[i-t-1]+z[j+t]-z[j]; if(im>k) continue; res1+=min(k-im,z[i-t-1]+z[n]-z[j+t]); res1=res1+b[i-t-1]+b[n]-b[j+t]; res=max(res,res1); } cout<<res<<"\n"; } |
English