#include <cstdio>
#include <vector>
#include <algorithm>
int main () {
int n, k, t, i, best, req, d1, d2, pot, spb;
char c;
scanf("%d %d %d\n", &n, &k, &t);
std::vector<int> P1(n + 8, 0), P2(n + 8, 0);
for (i = 0; i < n; ++i) {
P1[i + 1] = P1[i];
P2[i + 1] = P2[i];
scanf("%c", &c);
if (c == '1') {
++P1[i + 1];
}
if (c == '2') {
++P2[i + 1];
}
}
best = -1;
req = std::max(0, P1[n] + P2[n] - k);
if (P2[n] >= req) {
printf("%d\n", n - req);
return 0;
}
for (d1 = 0; d1 < n - 2 * t; ++d1) {
for (d2 = d1 + t + 1; d2 <= n - t; ++d2) {
spb = P1[d2] - P1[d1 + t] + P2[d2] - P2[d1 + t];
if (P2[d1] + spb + P2[n] - P2[d2 + t] < req) {
continue;
}
pot = d1 - d2 + n - t + spb - req;
if (pot > best) {
best = pot;
}
}
}
printf("%d\n", best);
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 | #include <cstdio> #include <vector> #include <algorithm> int main () { int n, k, t, i, best, req, d1, d2, pot, spb; char c; scanf("%d %d %d\n", &n, &k, &t); std::vector<int> P1(n + 8, 0), P2(n + 8, 0); for (i = 0; i < n; ++i) { P1[i + 1] = P1[i]; P2[i + 1] = P2[i]; scanf("%c", &c); if (c == '1') { ++P1[i + 1]; } if (c == '2') { ++P2[i + 1]; } } best = -1; req = std::max(0, P1[n] + P2[n] - k); if (P2[n] >= req) { printf("%d\n", n - req); return 0; } for (d1 = 0; d1 < n - 2 * t; ++d1) { for (d2 = d1 + t + 1; d2 <= n - t; ++d2) { spb = P1[d2] - P1[d1 + t] + P2[d2] - P2[d1 + t]; if (P2[d1] + spb + P2[n] - P2[d2 + t] < req) { continue; } pot = d1 - d2 + n - t + spb - req; if (pot > best) { best = pot; } } } printf("%d\n", best); return 0; } |
English