#include <bits/stdc++.h>
using namespace std;
int B[8100];
int Z[8100];
int Bt[8100];
int Zt[8100];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k, t;
cin>>n>>k>>t;
string slow;
cin>>slow;
slow = '&' + slow;
int pocz = 0;
int kon = 0;
int biuro = 0;
int zdalka = 0;
for(int i = 1; i < slow.size(); i++){
if(slow[i] == '1'){
biuro++;
}
if(slow[i] == '2'){
zdalka++;
}
B[i] = biuro;
Z[i] = zdalka;
}
int biurot = 0;
int zdalkat = 0;
for(int i = slow.size()-1; i>=1; i--){
if(slow[i] == '1'){
biurot++;
}
if(slow[i] == '2'){
zdalkat++;
}
Bt[i] = biurot;
Zt[i] = zdalkat;
}
if(biuro <= k){
cout<<slow.size() -1 - max((zdalka - (k - biuro)),0)<<"\n";
return 0;
}
int ma = -1;
int brakuje = biuro - k;
for(int i = 1; i< slow.size(); i++){
int biuro2 = 0;
int zdalka2 = 0;
for(int j = i; j < slow.size(); j++){
if(slow[j] == '1'){
biuro2++;
}
if(slow[j] == '2'){
zdalka2++;
}
if(i - t <=0 or j + t >= slow.size()){
continue;
}
int dom = Z[i - t - 1] + Zt[j + t +1];
int spotkan = biuro2 + zdalka2 + dom;
//cout<<biuro2<<" "<<zdalka2<<" "<<dom<<"\n";
if((biuro + zdalka) - spotkan <= k){
ma = max(ma, ((int)slow.size() - 1 - 2*t - (j - i +1) - max(dom - ( (biuro2 + zdalka2) - k), 0)));
//cout<<i<<" "<<j<<" "<<ma<<"\n";
}
}
}
cout<<ma<<"\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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | #include <bits/stdc++.h> using namespace std; int B[8100]; int Z[8100]; int Bt[8100]; int Zt[8100]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k, t; cin>>n>>k>>t; string slow; cin>>slow; slow = '&' + slow; int pocz = 0; int kon = 0; int biuro = 0; int zdalka = 0; for(int i = 1; i < slow.size(); i++){ if(slow[i] == '1'){ biuro++; } if(slow[i] == '2'){ zdalka++; } B[i] = biuro; Z[i] = zdalka; } int biurot = 0; int zdalkat = 0; for(int i = slow.size()-1; i>=1; i--){ if(slow[i] == '1'){ biurot++; } if(slow[i] == '2'){ zdalkat++; } Bt[i] = biurot; Zt[i] = zdalkat; } if(biuro <= k){ cout<<slow.size() -1 - max((zdalka - (k - biuro)),0)<<"\n"; return 0; } int ma = -1; int brakuje = biuro - k; for(int i = 1; i< slow.size(); i++){ int biuro2 = 0; int zdalka2 = 0; for(int j = i; j < slow.size(); j++){ if(slow[j] == '1'){ biuro2++; } if(slow[j] == '2'){ zdalka2++; } if(i - t <=0 or j + t >= slow.size()){ continue; } int dom = Z[i - t - 1] + Zt[j + t +1]; int spotkan = biuro2 + zdalka2 + dom; //cout<<biuro2<<" "<<zdalka2<<" "<<dom<<"\n"; if((biuro + zdalka) - spotkan <= k){ ma = max(ma, ((int)slow.size() - 1 - 2*t - (j - i +1) - max(dom - ( (biuro2 + zdalka2) - k), 0))); //cout<<i<<" "<<j<<" "<<ma<<"\n"; } } } cout<<ma<<"\n"; } |
English