#include <bits/stdc++.h>
using namespace std;
#define FOR(i,p,q) for(int i=(p); i<=(q); ++i)
#define ROF(i,p,q) for(int i=(p); i>=(q); --i)
#define REP(i,q) for(int i=0; i<(q); ++i)
#define pb push_back
#define as assign
#define rz resize
#define Co const
#define fi first
#define se second
#define all(X) X.begin(), X.end()
#define rall(X) X.rbegin(), X.rend()
#define sz(X) (int)(X.size())
#define ckmax(a,b) a=max(a,b)
#define ckmin(a,b) a=min(a,b)
#define V vector
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
#ifndef UNCLE
typedef basic_string<bool> vb;
typedef basic_string<int> vi;
typedef basic_string<ll> vl;
#else
typedef V<bool> vb;
typedef V<int> vi;
typedef V<ll> vl;
#endif
int N,K,T;
vi smP12, smP2;
void Input(){
string s;
cin>>N>>K>>T>>s;
smP12.as(N,0), smP2.as(N,0);
if(s[0]=='1') smP12[0]=1;
else if(s[0]=='2') smP12[0]=smP2[0]=1;
FOR(i,1,N-1){
if(s[i]=='1') smP12[i]=1;
else if(s[i]=='2') smP12[i]=smP2[i]=1;
smP12[i]+=smP12[i-1], smP2[i]+=smP2[i-1];
}
}
void Solve(){
int qt1=smP12[N-1]-smP2[N-1];
if(qt1<=K){ cout<<N-max(smP12[N-1]-K,0)<<"\n"; return;}
int res=-1;
FOR(q,T,N-T) FOR(p,0,q-T){
int kl=smP12[N-1]-K;
kl-=smP12[q-1]-smP12[p+T-1];
if(kl<0||((p?smP2[p-1]:0)+smP2[N-1]-smP2[q+T-1]<kl)) continue;
ckmax(res,N-(q+T-1-p+1)-kl);
}
cout<<res<<"\n";
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
Input();
Solve();
}
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #include <bits/stdc++.h> using namespace std; #define FOR(i,p,q) for(int i=(p); i<=(q); ++i) #define ROF(i,p,q) for(int i=(p); i>=(q); --i) #define REP(i,q) for(int i=0; i<(q); ++i) #define pb push_back #define as assign #define rz resize #define Co const #define fi first #define se second #define all(X) X.begin(), X.end() #define rall(X) X.rbegin(), X.rend() #define sz(X) (int)(X.size()) #define ckmax(a,b) a=max(a,b) #define ckmin(a,b) a=min(a,b) #define V vector typedef long long ll; typedef long double ld; typedef pair<int,int> pi; typedef pair<ll,ll> pl; #ifndef UNCLE typedef basic_string<bool> vb; typedef basic_string<int> vi; typedef basic_string<ll> vl; #else typedef V<bool> vb; typedef V<int> vi; typedef V<ll> vl; #endif int N,K,T; vi smP12, smP2; void Input(){ string s; cin>>N>>K>>T>>s; smP12.as(N,0), smP2.as(N,0); if(s[0]=='1') smP12[0]=1; else if(s[0]=='2') smP12[0]=smP2[0]=1; FOR(i,1,N-1){ if(s[i]=='1') smP12[i]=1; else if(s[i]=='2') smP12[i]=smP2[i]=1; smP12[i]+=smP12[i-1], smP2[i]+=smP2[i-1]; } } void Solve(){ int qt1=smP12[N-1]-smP2[N-1]; if(qt1<=K){ cout<<N-max(smP12[N-1]-K,0)<<"\n"; return;} int res=-1; FOR(q,T,N-T) FOR(p,0,q-T){ int kl=smP12[N-1]-K; kl-=smP12[q-1]-smP12[p+T-1]; if(kl<0||((p?smP2[p-1]:0)+smP2[N-1]-smP2[q+T-1]<kl)) continue; ckmax(res,N-(q+T-1-p+1)-kl); } cout<<res<<"\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); Input(); Solve(); } |
English