#include <bits/stdc++.h> using namespace std; #define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0); #define rep(i, n) for (int i = 0; i < (n); ++i) #define fwd(i, a, b) for (int i = (a); i < (b); ++i) #define all(c) (c).begin(), (c).end() #define sz(X) (int)((X).size()) #ifdef LOCAL ostream &operator<<(ostream &out, string str) { for (char c : str) out << c; return out; } template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.st << ", " << p.nd << ")"; } template <class L, class R, class S> ostream &operator<<(ostream &out, tuple<L, R, S> p) { auto &[a, b, c] = p; return out << "(" << a << ", " << b << ", " << c << ")"; } template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << '{'; for (auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? ", " : "") << *it; return out << '}'; } void dump() { cerr << "\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else #define debug(...) 42 #endif array<int, 8008> tab[3]; // sum in [a,b] int sum(int t, int a, int b) { return b < a ? 0 : (tab[t][b] - (a ? tab[t][a - 1] : 0)); } int sum01(int a, int b) { return sum(0, a, b) + sum(1, a, b); } int32_t main() { _upgrade; int n, k, t; cin >> n >> k >> t; debug(n, k, t); string s; cin >> s; rep(i, sz(s)) tab[s[i] - '1'][i]++; rep(i, 3) partial_sum(all(tab[i]), tab[i].begin()); int ans = -1; if (sum(0, 0, n - 1) <= k) ans = n - max(0, sum01(0, n - 1) - k); debug(ans); rep(i, n) fwd(j, i + 2 * t, n) { int pominiete = sum01(i, i + t - 1) + sum01(j - t + 1, j); int w_biurze = sum(0, 0, n - 1) - sum(0, i, j); debug(i, j, pominiete, w_biurze); if (pominiete + w_biurze <= k) { int zdalnie = sum01(0, n - 1) - sum01(i, j); int wolne = sum(2, 0, n - 1) - sum(2, i, j); int x = wolne + min(k - pominiete, zdalnie); debug(i, j, x); ans = max(ans, x); } } cout << ans << endl; }
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 | #include <bits/stdc++.h> using namespace std; #define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0); #define rep(i, n) for (int i = 0; i < (n); ++i) #define fwd(i, a, b) for (int i = (a); i < (b); ++i) #define all(c) (c).begin(), (c).end() #define sz(X) (int)((X).size()) #ifdef LOCAL ostream &operator<<(ostream &out, string str) { for (char c : str) out << c; return out; } template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.st << ", " << p.nd << ")"; } template <class L, class R, class S> ostream &operator<<(ostream &out, tuple<L, R, S> p) { auto &[a, b, c] = p; return out << "(" << a << ", " << b << ", " << c << ")"; } template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << '{'; for (auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? ", " : "") << *it; return out << '}'; } void dump() { cerr << "\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else #define debug(...) 42 #endif array<int, 8008> tab[3]; // sum in [a,b] int sum(int t, int a, int b) { return b < a ? 0 : (tab[t][b] - (a ? tab[t][a - 1] : 0)); } int sum01(int a, int b) { return sum(0, a, b) + sum(1, a, b); } int32_t main() { _upgrade; int n, k, t; cin >> n >> k >> t; debug(n, k, t); string s; cin >> s; rep(i, sz(s)) tab[s[i] - '1'][i]++; rep(i, 3) partial_sum(all(tab[i]), tab[i].begin()); int ans = -1; if (sum(0, 0, n - 1) <= k) ans = n - max(0, sum01(0, n - 1) - k); debug(ans); rep(i, n) fwd(j, i + 2 * t, n) { int pominiete = sum01(i, i + t - 1) + sum01(j - t + 1, j); int w_biurze = sum(0, 0, n - 1) - sum(0, i, j); debug(i, j, pominiete, w_biurze); if (pominiete + w_biurze <= k) { int zdalnie = sum01(0, n - 1) - sum01(i, j); int wolne = sum(2, 0, n - 1) - sum(2, i, j); int x = wolne + min(k - pominiete, zdalnie); debug(i, j, x); ans = max(ans, x); } } cout << ans << endl; } |