#include <iostream>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k, t;
cin >> n >> k >> t;
string day;
cin >> day;
int mx = -1;
int w = 0;
int x = 0;
for (int i = 0; i < n; i++) {
if (day[i] == '2') {
w++;
}
if (day[i] != '3') {
x++;
}
}
k = max(x - k, 0);
if (w >= k) {
mx = n - k;
}
for (int i = t; i < n; i++) {
int b = 0;
for (int l = 0; l < i - t; l++) {
if (day[l] == '2') {
b++;
}
}
int d = 0;
int a = 0;
for (int l = i + t; l < n; l++) {
if (day[l] == '2') {
a++;
}
}
for (int j = i; j < n - t + 1; j++) {
if (b + d + a >= k) {
mx = max(mx, n - (j - i) - 2 * t - (k - d));
}
if (day[j] != '3') {
d++;
}
if (j + t < n && day[j + t] == '2') {
a--;
}
}
}
cout << mx << endl;
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 | #include <iostream> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k, t; cin >> n >> k >> t; string day; cin >> day; int mx = -1; int w = 0; int x = 0; for (int i = 0; i < n; i++) { if (day[i] == '2') { w++; } if (day[i] != '3') { x++; } } k = max(x - k, 0); if (w >= k) { mx = n - k; } for (int i = t; i < n; i++) { int b = 0; for (int l = 0; l < i - t; l++) { if (day[l] == '2') { b++; } } int d = 0; int a = 0; for (int l = i + t; l < n; l++) { if (day[l] == '2') { a++; } } for (int j = i; j < n - t + 1; j++) { if (b + d + a >= k) { mx = max(mx, n - (j - i) - 2 * t - (k - d)); } if (day[j] != '3') { d++; } if (j + t < n && day[j + t] == '2') { a--; } } } cout << mx << endl; return 0; } |
English