#include <bits/stdc++.h>
using namespace std;
#define rep(a, b) for (int a = 0; a < (b); a++)
#define rep1(a, b) for (int a = 1; a <= (b); a++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int MOD = 1e9 + 7;
#define LOCAL false
const int LIM = 8007;
int n, k, t;
string str;
int pref[3][LIM];
int rcnt(int x, int l, int r) {
return pref[x][r]-pref[x][l-1];
}
int get(int l, int r) {
int wl = l+t;
int wr = r-t;
int rem = k-rcnt(1, l, wl-1)-rcnt(1, wr+1, r)-rcnt(0, 1, wl-1)-rcnt(0, wr+1, n);
if (rem < 0) return -1;
int work = rcnt(1, 1, l-1)+rcnt(1, r+1, n);
int tot = n-(r-l+1);
return tot-work+min(work, rem);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
if (LOCAL) {
ignore=freopen("io/in", "r", stdin);
ignore=freopen("io/out", "w", stdout);
}
cin >> n >> k >> t;
cin >> str;
rep1(i, (int)str.size()) {
rep(x, 3) pref[x][i] = pref[x][i-1];
pref[str[i-1]-'1'][i]++;
}
int ans = -1;
if (pref[0][n] <= k) ans = n-pref[1][n]+min(pref[1][n], k-pref[0][n]);
rep1(i, n) for (int j = i+2*t-1; j <= n; j++) ans = max(ans, get(i, j));
cout << ans << "\n";
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 | #include <bits/stdc++.h> using namespace std; #define rep(a, b) for (int a = 0; a < (b); a++) #define rep1(a, b) for (int a = 1; a <= (b); a++) #define all(x) (x).begin(), (x).end() using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; const int MOD = 1e9 + 7; #define LOCAL false const int LIM = 8007; int n, k, t; string str; int pref[3][LIM]; int rcnt(int x, int l, int r) { return pref[x][r]-pref[x][l-1]; } int get(int l, int r) { int wl = l+t; int wr = r-t; int rem = k-rcnt(1, l, wl-1)-rcnt(1, wr+1, r)-rcnt(0, 1, wl-1)-rcnt(0, wr+1, n); if (rem < 0) return -1; int work = rcnt(1, 1, l-1)+rcnt(1, r+1, n); int tot = n-(r-l+1); return tot-work+min(work, rem); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); if (LOCAL) { ignore=freopen("io/in", "r", stdin); ignore=freopen("io/out", "w", stdout); } cin >> n >> k >> t; cin >> str; rep1(i, (int)str.size()) { rep(x, 3) pref[x][i] = pref[x][i-1]; pref[str[i-1]-'1'][i]++; } int ans = -1; if (pref[0][n] <= k) ans = n-pref[1][n]+min(pref[1][n], k-pref[0][n]); rep1(i, n) for (int j = i+2*t-1; j <= n; j++) ans = max(ans, get(i, j)); cout << ans << "\n"; return 0; } |
English