#include <bits/stdc++.h>
using namespace std;
int tab[9000],prze;
int pref[9000],pref2[9000];
char ab;
int temp;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int wyn=-1;
int n, spo, drog;
cin>>n>>spo>>drog;
for(int i=1;i<=n;i++){
cin>>ab;
if(ab=='1'){
tab[i]=1;
}
if(ab=='2'){
tab[i]=2;
}
if(ab=='3'){
tab[i]=3;
}
}
for(int i=1;i<=n;i++){
pref2[i]=pref2[i-1];
if(tab[i]==2){
pref2[i]++;
}
}
for(int i=1;i<=n;i++){
pref[i]=pref[i-1];
if(tab[i]==2 || tab[i]==1){
pref[i]++;
}
}
int iletrzeba=pref[n];
iletrzeba-=spo;
if( iletrzeba<=pref2[n]){
cout<<n-max(0,iletrzeba);
return 0;
}
for(int i=1;i<=n;i++){
for(int j=i+1+drog;j<=n-drog+1;j++){
temp=pref2[i-1]+pref[j-1]-pref[i+drog-1]+pref2[n]-pref2[j+drog-1];
prze=pref[j-1]-pref[i+drog-1];
//cout<<i<<" "<<j<<" "<<temp<<endl;
if(prze>=iletrzeba){
// cout<<i<<" "<<j<<" "<<temp<<endl;
wyn=max(wyn, i-1+n-j-drog+1);
}
else{
if(temp>=iletrzeba){
//cout<<i<<" "<<j<<" "<<temp<<endl;
wyn=max(wyn, i-1+n-j-drog+1-(iletrzeba-prze));
}
}
}
}
cout<<wyn;
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 62 63 64 65 66 67 68 69 70 | #include <bits/stdc++.h> using namespace std; int tab[9000],prze; int pref[9000],pref2[9000]; char ab; int temp; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int wyn=-1; int n, spo, drog; cin>>n>>spo>>drog; for(int i=1;i<=n;i++){ cin>>ab; if(ab=='1'){ tab[i]=1; } if(ab=='2'){ tab[i]=2; } if(ab=='3'){ tab[i]=3; } } for(int i=1;i<=n;i++){ pref2[i]=pref2[i-1]; if(tab[i]==2){ pref2[i]++; } } for(int i=1;i<=n;i++){ pref[i]=pref[i-1]; if(tab[i]==2 || tab[i]==1){ pref[i]++; } } int iletrzeba=pref[n]; iletrzeba-=spo; if( iletrzeba<=pref2[n]){ cout<<n-max(0,iletrzeba); return 0; } for(int i=1;i<=n;i++){ for(int j=i+1+drog;j<=n-drog+1;j++){ temp=pref2[i-1]+pref[j-1]-pref[i+drog-1]+pref2[n]-pref2[j+drog-1]; prze=pref[j-1]-pref[i+drog-1]; //cout<<i<<" "<<j<<" "<<temp<<endl; if(prze>=iletrzeba){ // cout<<i<<" "<<j<<" "<<temp<<endl; wyn=max(wyn, i-1+n-j-drog+1); } else{ if(temp>=iletrzeba){ //cout<<i<<" "<<j<<" "<<temp<<endl; wyn=max(wyn, i-1+n-j-drog+1-(iletrzeba-prze)); } } } } cout<<wyn; return 0; } |
English