#include <bits/stdc++.h>
using namespace std;
const int N = 8007;
int n,k,t,ans = -1,kan;
int sum[N][6];
int trzeba[5],mozna[5];
int pol(int a,int b,int c){
return sum[b][c] - sum[a-1][c];
}
void solve(){
cin >> n >> k >> t;
for(int i = 1;i <= n;i++){
char c;
int a;
cin >> c;
a = c - '0';
sum[i][1] = sum[i-1][1];
sum[i][2] = sum[i-1][2];
sum[i][a]++;
}
if(k >= sum[n][1]){
cout << n - max(0,sum[n][1] + sum[n][2] - k) << "\n";
return;
}
for(int i = 1;i+2*t-1 <= n;i++){
for(int j = i+t;j+t-1<=n;j++){
trzeba[1] = pol(i,i+t-1,1) + pol(j,j+t-1,1);
trzeba[2] = pol(i,i+t-1,2) + pol(j,j+t-1,2);
mozna[1] = pol(1,i-1,1) + pol(j+t,n,1);
mozna[2] = pol(1,i-1,2) + pol(j+t,n,2);
if(k >= trzeba[1] + trzeba[2] + mozna[1]){
//cout << i << " " << j << " " << ans << " " << trzeba[1] << " " << trzeba[2] << " " << mozna[1] << " " << mozna[2] << "\n";
ans = max(ans,n - (j+t-i) - max(0,trzeba[1] + trzeba[2] + mozna[1] + mozna[2] - k));
}
}
}
cout << ans;
}
int main(){
iostream::sync_with_stdio(0);
cin.tie(0);
int t = 1;
//cin >> t;
while(t--){
solve();
}
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 | #include <bits/stdc++.h> using namespace std; const int N = 8007; int n,k,t,ans = -1,kan; int sum[N][6]; int trzeba[5],mozna[5]; int pol(int a,int b,int c){ return sum[b][c] - sum[a-1][c]; } void solve(){ cin >> n >> k >> t; for(int i = 1;i <= n;i++){ char c; int a; cin >> c; a = c - '0'; sum[i][1] = sum[i-1][1]; sum[i][2] = sum[i-1][2]; sum[i][a]++; } if(k >= sum[n][1]){ cout << n - max(0,sum[n][1] + sum[n][2] - k) << "\n"; return; } for(int i = 1;i+2*t-1 <= n;i++){ for(int j = i+t;j+t-1<=n;j++){ trzeba[1] = pol(i,i+t-1,1) + pol(j,j+t-1,1); trzeba[2] = pol(i,i+t-1,2) + pol(j,j+t-1,2); mozna[1] = pol(1,i-1,1) + pol(j+t,n,1); mozna[2] = pol(1,i-1,2) + pol(j+t,n,2); if(k >= trzeba[1] + trzeba[2] + mozna[1]){ //cout << i << " " << j << " " << ans << " " << trzeba[1] << " " << trzeba[2] << " " << mozna[1] << " " << mozna[2] << "\n"; ans = max(ans,n - (j+t-i) - max(0,trzeba[1] + trzeba[2] + mozna[1] + mozna[2] - k)); } } } cout << ans; } int main(){ iostream::sync_with_stdio(0); cin.tie(0); int t = 1; //cin >> t; while(t--){ solve(); } return 0; } |
English