#include <iostream>
using namespace std;
const int N = 8009;
int cnt[4][N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k, t; cin >> n >> k >> t;
string s; cin >> s;
for(int i = 1; i <= n; ++i) {
cnt[1][i] = cnt[1][i-1]+(s[i-1] == '1');
cnt[2][i] = cnt[2][i-1]+(s[i-1] == '2');
cnt[3][i] = cnt[3][i-1]+(s[i-1] == '3');
}
int res = -1;
{
int x = cnt[1][n];
if(x <= k) {
int home_hours = n;
int wfh = cnt[2][n];
wfh = max(wfh-(k-x), 0);
int y = home_hours-wfh;
res = max(res, y);
}
}
for(int a = t+1; a <= n-t; ++a) {
for(int b = a; b <= n-t; ++b) {
int to_office1 = cnt[1][a-1]-cnt[1][a-t-1];
int to_office2 = cnt[2][a-1]-cnt[2][a-t-1];
int from_office1 = cnt[1][b+t]-cnt[1][b];
int from_office2 = cnt[2][b+t]-cnt[2][b];
int home_before = cnt[1][a-t-1]-cnt[1][0];
int home_after = cnt[1][n]-cnt[1][b+t];
int x = to_office1+to_office2;
x += from_office1+from_office2;
x += home_before+home_after;
if(x <= k) {
int home_hours = n-(b-a+1+t+t);
int wfh = cnt[2][a-t-1]-cnt[2][0]+cnt[2][n]-cnt[2][b+t];
wfh = max(wfh-(k-x), 0);
int y = home_hours-wfh;
res = max(res, y);
}
}
}
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 58 59 60 | #include <iostream> using namespace std; const int N = 8009; int cnt[4][N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k, t; cin >> n >> k >> t; string s; cin >> s; for(int i = 1; i <= n; ++i) { cnt[1][i] = cnt[1][i-1]+(s[i-1] == '1'); cnt[2][i] = cnt[2][i-1]+(s[i-1] == '2'); cnt[3][i] = cnt[3][i-1]+(s[i-1] == '3'); } int res = -1; { int x = cnt[1][n]; if(x <= k) { int home_hours = n; int wfh = cnt[2][n]; wfh = max(wfh-(k-x), 0); int y = home_hours-wfh; res = max(res, y); } } for(int a = t+1; a <= n-t; ++a) { for(int b = a; b <= n-t; ++b) { int to_office1 = cnt[1][a-1]-cnt[1][a-t-1]; int to_office2 = cnt[2][a-1]-cnt[2][a-t-1]; int from_office1 = cnt[1][b+t]-cnt[1][b]; int from_office2 = cnt[2][b+t]-cnt[2][b]; int home_before = cnt[1][a-t-1]-cnt[1][0]; int home_after = cnt[1][n]-cnt[1][b+t]; int x = to_office1+to_office2; x += from_office1+from_office2; x += home_before+home_after; if(x <= k) { int home_hours = n-(b-a+1+t+t); int wfh = cnt[2][a-t-1]-cnt[2][0]+cnt[2][n]-cnt[2][b+t]; wfh = max(wfh-(k-x), 0); int y = home_hours-wfh; res = max(res, y); } } } cout << res << "\n"; return 0; } |
English