#include <bits/stdc++.h>
using namespace std;
int a[8080];
int ps[4][8080];
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;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= 3; j++) {
ps[j][i] = ps[j][i - 1] + (s[i - 1] - '0' == j);
}
}
if(ps[1][n] <= k) {
k -= ps[1][n];
cout << n - max(0, ps[2][n] - k) << "\n";
return 0;
}
int res = -1;
for(int i = 1; i <= n; i++) {
for(int j = i + t; j + t <= n + 1; j++) {
int tmp_k = k;
int way_meetings = 0;
for(int k = 1; k <= 2; k++) {
way_meetings += (ps[k][i + t - 1] - ps[k][i - 1]) + (ps[k][j + t - 1] - ps[k][j - 1]);
}
int home_onsite_meetings = 0;
home_onsite_meetings += (ps[1][i - 1] - ps[1][0]) + (ps[1][n] - ps[1][j + t - 1]);
int c = home_onsite_meetings;
tmp_k -= way_meetings;
tmp_k -= home_onsite_meetings;
if(tmp_k < 0) {
continue;
}
c += (ps[3][i - 1] - ps[3][0]) + (ps[3][n] - ps[3][j + t - 1]);
res = max(res, c);
}
}
cout << res << "\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 | #include <bits/stdc++.h> using namespace std; int a[8080]; int ps[4][8080]; 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; for(int i = 1; i <= n; i++) { for(int j = 1; j <= 3; j++) { ps[j][i] = ps[j][i - 1] + (s[i - 1] - '0' == j); } } if(ps[1][n] <= k) { k -= ps[1][n]; cout << n - max(0, ps[2][n] - k) << "\n"; return 0; } int res = -1; for(int i = 1; i <= n; i++) { for(int j = i + t; j + t <= n + 1; j++) { int tmp_k = k; int way_meetings = 0; for(int k = 1; k <= 2; k++) { way_meetings += (ps[k][i + t - 1] - ps[k][i - 1]) + (ps[k][j + t - 1] - ps[k][j - 1]); } int home_onsite_meetings = 0; home_onsite_meetings += (ps[1][i - 1] - ps[1][0]) + (ps[1][n] - ps[1][j + t - 1]); int c = home_onsite_meetings; tmp_k -= way_meetings; tmp_k -= home_onsite_meetings; if(tmp_k < 0) { continue; } c += (ps[3][i - 1] - ps[3][0]) + (ps[3][n] - ps[3][j + t - 1]); res = max(res, c); } } cout << res << "\n"; return 0; } |
English