#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (n); i++) #define pb push_back #define st first #define nd second const int MAXN = 1e4 + 7; int pref[MAXN][3]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k, t; cin >> n >> k >> t; string s; cin >> s; rep(i, n) { rep(c, 3) { pref[i + 1][c] = pref[i][c]; } int c = s[i] - '1'; pref[i + 1][c]++; } int ans = -1; if (pref[n][0] <= k) { ans = max(ans, pref[n][0] + pref[n][2] + min(k - pref[n][0], pref[n][1])); } for (int i = t; i < n; i++) { for (int j = i; j < n - t; j++) { int ile = 0; ile += (pref[i][0] - pref[i - t][0]) + (pref[i][1] - pref[i - t][1]); ile += (pref[j + t + 1][0] - pref[j + 1][0]) + (pref[j + t + 1][1] - pref[j + 1][1]); // cout << "ile = " << ile << '\n'; ile += pref[i - t][0]; ile += pref[n][0] - pref[j + t + 1][0]; int suma = pref[i - t][0] + pref[i - t][2] + (pref[n][0] + pref[n][2] - pref[j + t + 1][0] - pref[j + t + 1][2]); int pom = pref[i - t][1] + (pref[n][1] - pref[j + t + 1][1]); // cout << "i = " << i << " j = " << j << " suma = " << suma << " lef = " << k - ile << " pom = " << pom << '\n'; if (ile <= k) { int lef = k - ile; ans = max(ans, suma + min(lef, pom)); } } } 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (n); i++) #define pb push_back #define st first #define nd second const int MAXN = 1e4 + 7; int pref[MAXN][3]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k, t; cin >> n >> k >> t; string s; cin >> s; rep(i, n) { rep(c, 3) { pref[i + 1][c] = pref[i][c]; } int c = s[i] - '1'; pref[i + 1][c]++; } int ans = -1; if (pref[n][0] <= k) { ans = max(ans, pref[n][0] + pref[n][2] + min(k - pref[n][0], pref[n][1])); } for (int i = t; i < n; i++) { for (int j = i; j < n - t; j++) { int ile = 0; ile += (pref[i][0] - pref[i - t][0]) + (pref[i][1] - pref[i - t][1]); ile += (pref[j + t + 1][0] - pref[j + 1][0]) + (pref[j + t + 1][1] - pref[j + 1][1]); // cout << "ile = " << ile << '\n'; ile += pref[i - t][0]; ile += pref[n][0] - pref[j + t + 1][0]; int suma = pref[i - t][0] + pref[i - t][2] + (pref[n][0] + pref[n][2] - pref[j + t + 1][0] - pref[j + t + 1][2]); int pom = pref[i - t][1] + (pref[n][1] - pref[j + t + 1][1]); // cout << "i = " << i << " j = " << j << " suma = " << suma << " lef = " << k - ile << " pom = " << pom << '\n'; if (ile <= k) { int lef = k - ile; ans = max(ans, suma + min(lef, pom)); } } } cout << ans << '\n'; return 0; } |