#include <cstdio> #include <vector> #include <utility> using namespace std; const int N = 8005; char s[N]; int acc[2][N]; int main() { int n, k, t; scanf("%d%d%d", &n, &k, &t); scanf("%s", s + 1); for (int i = 1; i <= n; i++) { int c = s[i] - '1'; for (int j = 0; j < 2; j++) { acc[j][i] = acc[j][i - 1]; } if (c < 2) { acc[c][i]++; } } int nw = max(0, acc[0][n] + acc[1][n] - k); int first = t + 1; int last = n - t; int res = -1; for (int a = first; a <= last; a++) { for (int b = a; b <= last; b++) { int wd = (acc[0][b] - acc[0][a - 1]) + (acc[1][b] - acc[1][a - 1]); int pw = acc[1][a - t - 1] + (acc[1][n] - acc[1][b + t]); int l = n - (b - a + 1) - 2 * t; int aw = max(0, nw - wd); if (aw > pw) { continue; } res = max(res, l - aw); } } int pw = acc[1][n]; if (nw <= pw) { res = max(res, n - nw); } printf("%d\n", res); } // g++ -std=c++17 -Wall -Wextra -Wshadow B.cpp -o B
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 | #include <cstdio> #include <vector> #include <utility> using namespace std; const int N = 8005; char s[N]; int acc[2][N]; int main() { int n, k, t; scanf("%d%d%d", &n, &k, &t); scanf("%s", s + 1); for (int i = 1; i <= n; i++) { int c = s[i] - '1'; for (int j = 0; j < 2; j++) { acc[j][i] = acc[j][i - 1]; } if (c < 2) { acc[c][i]++; } } int nw = max(0, acc[0][n] + acc[1][n] - k); int first = t + 1; int last = n - t; int res = -1; for (int a = first; a <= last; a++) { for (int b = a; b <= last; b++) { int wd = (acc[0][b] - acc[0][a - 1]) + (acc[1][b] - acc[1][a - 1]); int pw = acc[1][a - t - 1] + (acc[1][n] - acc[1][b + t]); int l = n - (b - a + 1) - 2 * t; int aw = max(0, nw - wd); if (aw > pw) { continue; } res = max(res, l - aw); } } int pw = acc[1][n]; if (nw <= pw) { res = max(res, n - nw); } printf("%d\n", res); } // g++ -std=c++17 -Wall -Wextra -Wshadow B.cpp -o B |