#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "debug.hpp"
#else
#define debug(...) (void)0
#endif
namespace R = ranges;
namespace V = views;
auto ra(auto x, auto y) { return R::iota_view(x, y); }
auto rae(auto x, auto y) { return ra(x, y + 1); }
using i64 = int64_t;
void solve() {
int n, k, t;
cin >> n >> k >> t;
string s;
cin >> s;
vector sum(3, vector<int>(n + 1));
for (auto i : ra(0, n)) {
int tp = s[i] - '1';
sum[tp][i + 1] = 1;
}
for (auto i : ra(0, 3)) {
for (auto j : rae(1, n)) {
sum[i][j] += sum[i][j - 1];
}
}
auto local = [&](int l, int r) {
return l > r ? 0 : sum[0][r + 1] - sum[0][l];
};
auto remote = [&](int l, int r) {
return l > r ? 0 : sum[1][r + 1] - sum[1][l];
};
auto free = [&](int l, int r) {
return l > r ? 0 : sum[2][r + 1] - sum[2][l];
};
auto any = [&](int l, int r) {
return l > r ? 0 : local(l, r) + remote(l, r);
};
int ans = -1;
for (auto i : ra(0, n)) {
for (auto j : ra(i, n)) {
if (j - i + 1 < 2 * t) {
continue;
}
int forced = any(i, i + t - 1) + any(j - t + 1, j) + local(0, i - 1) +
local(j + 1, n - 1);
if (forced > k) {
continue;
}
int can_skip = k - forced;
ans =
max(ans, free(0, i - 1) + free(j + 1, n - 1) + local(0, i - 1) +
local(j + 1, n - 1) +
min(can_skip, remote(0, i - 1) + remote(j + 1, n - 1)));
}
}
{
int forced = local(0, n - 1);
if (forced <= k) {
int can_skip = k - forced;
ans = max(ans, free(0, n - 1) + local(0, n - 1) +
min(can_skip, remote(0, n - 1)));
}
}
cout << ans << endl;
}
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(false);
int t = 1;
// cin >> t;
for (auto tc_n : ra(0, t)) {
debug(tc_n);
solve();
cout.flush();
}
}
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 75 76 77 78 79 80 81 82 83 | #include <bits/stdc++.h> using namespace std; #ifdef DEBUG #include "debug.hpp" #else #define debug(...) (void)0 #endif namespace R = ranges; namespace V = views; auto ra(auto x, auto y) { return R::iota_view(x, y); } auto rae(auto x, auto y) { return ra(x, y + 1); } using i64 = int64_t; void solve() { int n, k, t; cin >> n >> k >> t; string s; cin >> s; vector sum(3, vector<int>(n + 1)); for (auto i : ra(0, n)) { int tp = s[i] - '1'; sum[tp][i + 1] = 1; } for (auto i : ra(0, 3)) { for (auto j : rae(1, n)) { sum[i][j] += sum[i][j - 1]; } } auto local = [&](int l, int r) { return l > r ? 0 : sum[0][r + 1] - sum[0][l]; }; auto remote = [&](int l, int r) { return l > r ? 0 : sum[1][r + 1] - sum[1][l]; }; auto free = [&](int l, int r) { return l > r ? 0 : sum[2][r + 1] - sum[2][l]; }; auto any = [&](int l, int r) { return l > r ? 0 : local(l, r) + remote(l, r); }; int ans = -1; for (auto i : ra(0, n)) { for (auto j : ra(i, n)) { if (j - i + 1 < 2 * t) { continue; } int forced = any(i, i + t - 1) + any(j - t + 1, j) + local(0, i - 1) + local(j + 1, n - 1); if (forced > k) { continue; } int can_skip = k - forced; ans = max(ans, free(0, i - 1) + free(j + 1, n - 1) + local(0, i - 1) + local(j + 1, n - 1) + min(can_skip, remote(0, i - 1) + remote(j + 1, n - 1))); } } { int forced = local(0, n - 1); if (forced <= k) { int can_skip = k - forced; ans = max(ans, free(0, n - 1) + local(0, n - 1) + min(can_skip, remote(0, n - 1))); } } cout << ans << endl; } int32_t main() { cin.tie(nullptr)->sync_with_stdio(false); int t = 1; // cin >> t; for (auto tc_n : ra(0, t)) { debug(tc_n); solve(); cout.flush(); } } |
English