/// UH Top #include <bits/stdc++.h> #define db(x) cerr << #x << ':' << (x) << '\n'; #define all(v) (v).begin(), (v).end() #define allr(v) (v).rbegin(), (v).rend() // #define int ll using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; // typedef __int128_t int128; typedef pair<ll, ll> pii; typedef pair<ld, ll> pdi; typedef pair<ld, ld> pdd; typedef pair<ld, pdd> pdp; typedef pair<string, ll> psi; typedef pair<ll, string> pls; typedef pair<string, string> pss; typedef pair<ll, pii> pip; typedef pair<pii, pii> ppp; typedef complex<ld> point; typedef vector<point> polygon; typedef vector<ll> vi; typedef pair<point, int> ppi; #define prec(n) \ cout.precision(n); \ cout << fixed const ll mod = (1e9 + 7); const ld eps = (1e-9); const ll oo = (ll)(1e9 + 5); #define pi acos(-1) #define MAXN (ll)(1e6 + 5) int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k, t; cin >> n >> k >> t; vector<int> a(n); vector<vector<int>> ac(3, vector<int>(n + 1)); for (int i = 0; i < n; i++) { char c; cin >> c; a[i] = c - '1'; ac[0][i + 1] = ac[0][i] + (a[i] == 0); ac[1][i + 1] = ac[1][i] + (a[i] == 1); ac[2][i + 1] = ac[2][i] + (a[i] == 2); } int c1 = ac[0][n]; int c2 = ac[1][n]; int c3 = ac[2][n]; if (c1 <= k) { int couldMiss2 = k - c1; int haveToAttend2 = max(c2 - couldMiss2, 0); cout << n - haveToAttend2 << '\n'; return 0; } auto unattended = [&](int l, int r, int type) { return ac[type][r] - ac[type][l]; }; // c1 > k int ans = -1; for (int i = 0; i + t <= n; i++) { for (int j = i + t; j + t <= n; j++) { int k1 = i + t; int l = j + t; int unattended0 = unattended(0, k1, 0) + unattended(j, n, 0); int unattended1 = unattended(i, k1, 1) + unattended(j, l, 1); // if (i == 3 && j == 6) { // db(unattended0); // db(unattended1); // } int couldMiss2 = k - unattended0 - unattended1; if (couldMiss2 < 0) { continue; } int haveToAttend2 = max(unattended(0, i, 1) + unattended(l, n, 1) - couldMiss2, 0); // if (i == 3 && j == 6) { // db(couldMiss2); // db(haveToAttend2); // } ans = max(ans, i + (n - l) - haveToAttend2); } } 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 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 84 85 86 87 88 89 90 91 92 93 94 | /// UH Top #include <bits/stdc++.h> #define db(x) cerr << #x << ':' << (x) << '\n'; #define all(v) (v).begin(), (v).end() #define allr(v) (v).rbegin(), (v).rend() // #define int ll using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; // typedef __int128_t int128; typedef pair<ll, ll> pii; typedef pair<ld, ll> pdi; typedef pair<ld, ld> pdd; typedef pair<ld, pdd> pdp; typedef pair<string, ll> psi; typedef pair<ll, string> pls; typedef pair<string, string> pss; typedef pair<ll, pii> pip; typedef pair<pii, pii> ppp; typedef complex<ld> point; typedef vector<point> polygon; typedef vector<ll> vi; typedef pair<point, int> ppi; #define prec(n) \ cout.precision(n); \ cout << fixed const ll mod = (1e9 + 7); const ld eps = (1e-9); const ll oo = (ll)(1e9 + 5); #define pi acos(-1) #define MAXN (ll)(1e6 + 5) int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k, t; cin >> n >> k >> t; vector<int> a(n); vector<vector<int>> ac(3, vector<int>(n + 1)); for (int i = 0; i < n; i++) { char c; cin >> c; a[i] = c - '1'; ac[0][i + 1] = ac[0][i] + (a[i] == 0); ac[1][i + 1] = ac[1][i] + (a[i] == 1); ac[2][i + 1] = ac[2][i] + (a[i] == 2); } int c1 = ac[0][n]; int c2 = ac[1][n]; int c3 = ac[2][n]; if (c1 <= k) { int couldMiss2 = k - c1; int haveToAttend2 = max(c2 - couldMiss2, 0); cout << n - haveToAttend2 << '\n'; return 0; } auto unattended = [&](int l, int r, int type) { return ac[type][r] - ac[type][l]; }; // c1 > k int ans = -1; for (int i = 0; i + t <= n; i++) { for (int j = i + t; j + t <= n; j++) { int k1 = i + t; int l = j + t; int unattended0 = unattended(0, k1, 0) + unattended(j, n, 0); int unattended1 = unattended(i, k1, 1) + unattended(j, l, 1); // if (i == 3 && j == 6) { // db(unattended0); // db(unattended1); // } int couldMiss2 = k - unattended0 - unattended1; if (couldMiss2 < 0) { continue; } int haveToAttend2 = max(unattended(0, i, 1) + unattended(l, n, 1) - couldMiss2, 0); // if (i == 3 && j == 6) { // db(couldMiss2); // db(haveToAttend2); // } ans = max(ans, i + (n - l) - haveToAttend2); } } cout << ans << "\n"; return 0; } |