#include <bits/stdc++.h>
#define ll long long
#define ve vector
#define fi first
#define se second
#define ld long double
#define all(x) x.begin(), x.end()
using namespace std;
typedef pair<int, int> pii;
const int MAXN = 1e4+10;
int pref[MAXN][3];
int get(int l, int r, int kind){
return pref[r][kind] - pref[l - 1][kind];
}
//int testnum = 0;
void solve(){
// testnum++;
// ifstream cin((to_string(testnum) + ".in").c_str());
// ifstream fin((to_string(testnum) + ".out").c_str());
// int right;
// fin >> right;
memset(pref, 0, sizeof(pref));
// cout << testnum << ":\n";
int n, k, t;
cin >> n >> k >> t;
for(int i = 1; i <= n; i++){
char x;
cin >> x;
memcpy(pref[i], pref[i - 1], sizeof(pref[i]));
pref[i][x - '1']++;
}
int res = -1;
if(get(1, n, 0) <= k){
cout << n + min(0, k - get(1, n, 0) - get(1, n, 1)) << "\n";
// assert(right == n + min(0, k - get(1, n, 0) - get(1, n, 1)) );
return;
}
for(int i = 1; i + 2*t <= n+1; i++)
for(int j = i + t; j + t <= n+1; j++){
int cnt = get(1, i + t - 1, 0) + get(j, n, 0) + get(i, i + t - 1, 1) + get(j, j + t - 1, 1);
if(cnt <= k){
res = max(res, get(1, i - 1, 2) + get(j + t, n, 2) + k - cnt + get(1, i - 1, 0) + get(j + t, n, 0));
}
}
cout << max(-1, min(n - 2*t, res)) << "\n";
// assert(right == max(-1, min(n - 2*t, res)));
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int T= 1;
// cin >> T;
while(T--)
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 | #include <bits/stdc++.h> #define ll long long #define ve vector #define fi first #define se second #define ld long double #define all(x) x.begin(), x.end() using namespace std; typedef pair<int, int> pii; const int MAXN = 1e4+10; int pref[MAXN][3]; int get(int l, int r, int kind){ return pref[r][kind] - pref[l - 1][kind]; } //int testnum = 0; void solve(){ // testnum++; // ifstream cin((to_string(testnum) + ".in").c_str()); // ifstream fin((to_string(testnum) + ".out").c_str()); // int right; // fin >> right; memset(pref, 0, sizeof(pref)); // cout << testnum << ":\n"; int n, k, t; cin >> n >> k >> t; for(int i = 1; i <= n; i++){ char x; cin >> x; memcpy(pref[i], pref[i - 1], sizeof(pref[i])); pref[i][x - '1']++; } int res = -1; if(get(1, n, 0) <= k){ cout << n + min(0, k - get(1, n, 0) - get(1, n, 1)) << "\n"; // assert(right == n + min(0, k - get(1, n, 0) - get(1, n, 1)) ); return; } for(int i = 1; i + 2*t <= n+1; i++) for(int j = i + t; j + t <= n+1; j++){ int cnt = get(1, i + t - 1, 0) + get(j, n, 0) + get(i, i + t - 1, 1) + get(j, j + t - 1, 1); if(cnt <= k){ res = max(res, get(1, i - 1, 2) + get(j + t, n, 2) + k - cnt + get(1, i - 1, 0) + get(j + t, n, 0)); } } cout << max(-1, min(n - 2*t, res)) << "\n"; // assert(right == max(-1, min(n - 2*t, res))); } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); int T= 1; // cin >> T; while(T--) solve(); } |
English