#include <bits/stdc++.h>
#define rep(i, j, k) for (int i = (j); i <= (k); ++i)
#define per(i, j, k) for (int i = (j); i >= (k); --i)
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
using ll = long long;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;
template<class T> void chkmn(T &x, T y) { if (y < x) x = y; }
template<class T> void chkmx(T &x, T y) { if (y > x) x = y; }
using namespace std;
const int maxn = 10010;
int n, k, t, a[maxn], cnt[maxn][3];
string str;
int main() {
cin.tie(nullptr) -> ios::sync_with_stdio(false);
cin >> n >> k >> t >> str;
rep (i, 1, n) a[i] = str[i - 1] - '1';
rep (i, 1, n) rep (j, 0, 2) cnt[i][j] = cnt[i - 1][j] + (a[i] == j);
int ans = -1;
rep (l1, 1, n - t + 1) rep (l2, l1 + t, n - t + 1) {
int r1 = l1 + t - 1, r2 = l2 + t - 1, ban = 0;
ban += cnt[r1][0] - cnt[l1 - 1][0] + cnt[r1][1] - cnt[l1 - 1][1];
ban += cnt[r2][0] - cnt[l2 - 1][0] + cnt[r2][1] - cnt[l2 - 1][1];
ban += cnt[l1 - 1][0] + cnt[n][0] - cnt[r2][0];
if (ban > k) continue;
int rest = 0, hw = 0;
rest += cnt[l1 - 1][2] + cnt[n][2] - cnt[r2][2] + cnt[l1 - 1][0] + cnt[n][0] - cnt[r2][0];
hw += cnt[l1 - 1][1] + cnt[n][1] - cnt[r2][1];
chkmx(ans, rest + min(hw, k - ban));
}
if (cnt[n][0] <= k) chkmx(ans, cnt[n][2] + cnt[n][0] + min(cnt[n][1], k - cnt[n][0]));
cout << ans << '\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 | #include <bits/stdc++.h> #define rep(i, j, k) for (int i = (j); i <= (k); ++i) #define per(i, j, k) for (int i = (j); i >= (k); --i) #define SZ(v) int((v).size()) #define ALL(v) (v).begin(),(v).end() #define fi first #define se second using ll = long long; using pii = std::pair<int, int>; using pll = std::pair<ll, ll>; template<class T> void chkmn(T &x, T y) { if (y < x) x = y; } template<class T> void chkmx(T &x, T y) { if (y > x) x = y; } using namespace std; const int maxn = 10010; int n, k, t, a[maxn], cnt[maxn][3]; string str; int main() { cin.tie(nullptr) -> ios::sync_with_stdio(false); cin >> n >> k >> t >> str; rep (i, 1, n) a[i] = str[i - 1] - '1'; rep (i, 1, n) rep (j, 0, 2) cnt[i][j] = cnt[i - 1][j] + (a[i] == j); int ans = -1; rep (l1, 1, n - t + 1) rep (l2, l1 + t, n - t + 1) { int r1 = l1 + t - 1, r2 = l2 + t - 1, ban = 0; ban += cnt[r1][0] - cnt[l1 - 1][0] + cnt[r1][1] - cnt[l1 - 1][1]; ban += cnt[r2][0] - cnt[l2 - 1][0] + cnt[r2][1] - cnt[l2 - 1][1]; ban += cnt[l1 - 1][0] + cnt[n][0] - cnt[r2][0]; if (ban > k) continue; int rest = 0, hw = 0; rest += cnt[l1 - 1][2] + cnt[n][2] - cnt[r2][2] + cnt[l1 - 1][0] + cnt[n][0] - cnt[r2][0]; hw += cnt[l1 - 1][1] + cnt[n][1] - cnt[r2][1]; chkmx(ans, rest + min(hw, k - ban)); } if (cnt[n][0] <= k) chkmx(ans, cnt[n][2] + cnt[n][0] + min(cnt[n][1], k - cnt[n][0])); cout << ans << '\n'; } |
English