#include <bits/stdc++.h> using namespace std; const int N=8e4; int n,k,t,odp=-1,trz; string wcz; int pra[N+4], prb[N+4], prc[N+4]; void sprawdz(int l, int r) { int wpr=0, dom=0; wpr= pra[r-1]-pra[l+t-1] + prb[r-1]-prb[l+t-1]; dom = prb[l-1]+prb[n]-prb[r+t-1]; if(wpr+dom<trz) return; if(wpr<trz) { odp=max(odp, n-(r+t-l)-(trz-wpr)); //cout<<l<<" "<<r<<"\n"; } else { odp=max(odp, n-(r+t-l)); //cout<<l<<" "<<r<<"$\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>n>>k>>t>>wcz; for(int i=1;i<=n;i++) { pra[i]=pra[i-1]; prb[i]=prb[i-1]; prc[i]=prc[i-1]; if(wcz[i-1]=='1') pra[i]++; else if(wcz[i-1]=='2') prb[i]++; else prc[i]++; } if(pra[n]<=k) { cout<<n-max(0,(pra[n]+prb[n]-k)); return 0; } trz=pra[n]+prb[n]-k; for(int l=1;l<=n-2*t;l++) { for(int r=l+2;r<=n-t+1; r++) { sprawdz(l,r); } } cout<<odp; 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #include <bits/stdc++.h> using namespace std; const int N=8e4; int n,k,t,odp=-1,trz; string wcz; int pra[N+4], prb[N+4], prc[N+4]; void sprawdz(int l, int r) { int wpr=0, dom=0; wpr= pra[r-1]-pra[l+t-1] + prb[r-1]-prb[l+t-1]; dom = prb[l-1]+prb[n]-prb[r+t-1]; if(wpr+dom<trz) return; if(wpr<trz) { odp=max(odp, n-(r+t-l)-(trz-wpr)); //cout<<l<<" "<<r<<"\n"; } else { odp=max(odp, n-(r+t-l)); //cout<<l<<" "<<r<<"$\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>n>>k>>t>>wcz; for(int i=1;i<=n;i++) { pra[i]=pra[i-1]; prb[i]=prb[i-1]; prc[i]=prc[i-1]; if(wcz[i-1]=='1') pra[i]++; else if(wcz[i-1]=='2') prb[i]++; else prc[i]++; } if(pra[n]<=k) { cout<<n-max(0,(pra[n]+prb[n]-k)); return 0; } trz=pra[n]+prb[n]-k; for(int l=1;l<=n-2*t;l++) { for(int r=l+2;r<=n-t+1; r++) { sprawdz(l,r); } } cout<<odp; return 0; } |