#include <iostream>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k, t, res = -1;
cin>>n>>k>>t;
char c[n + 1];
int tab1[n + 1], tab2[n + 1], tab3[n + 1], temp;
tab1[0] = tab2[0] = tab3[0] = 0;
for(int i = 1; i <= n; i++) {
cin>>c[i];
if(c[i] == '1') tab1[i] = tab1[i - 1] + 1;
else tab1[i] = tab1[i - 1];
if(c[i] == '2') tab2[i] = tab2[i - 1] + 1;
else tab2[i] = tab2[i - 1];
if(c[i] == '3') tab3[i] = tab3[i - 1] + 1;
else tab3[i] = tab3[i - 1];
}
if(k >= tab1[n]) {
cout<<min(tab3[n] + k, n);
return 0;
}
else {
for(int i = 1; i <= n - 2*t; i++) {
for(int j = i + 2*t; j <= n; j++) {
temp = - (tab2[j] - tab2[j - t]) - (tab2[i + t - 1] - tab2[i - 1]) + tab1[j - t] - tab1[i + t - 1];
if(temp >= tab1[n] - k) {
res = max(res, tab1[n] - (tab1[j] - tab1[i - 1]) + tab3[n] - (tab3[j] - tab3[i - 1]) + min(temp - (tab1[n] - k), tab2[n] - (tab2[j] - tab2[i - 1])));
}
}
}
cout<<res;
}
}
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 | #include <iostream> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k, t, res = -1; cin>>n>>k>>t; char c[n + 1]; int tab1[n + 1], tab2[n + 1], tab3[n + 1], temp; tab1[0] = tab2[0] = tab3[0] = 0; for(int i = 1; i <= n; i++) { cin>>c[i]; if(c[i] == '1') tab1[i] = tab1[i - 1] + 1; else tab1[i] = tab1[i - 1]; if(c[i] == '2') tab2[i] = tab2[i - 1] + 1; else tab2[i] = tab2[i - 1]; if(c[i] == '3') tab3[i] = tab3[i - 1] + 1; else tab3[i] = tab3[i - 1]; } if(k >= tab1[n]) { cout<<min(tab3[n] + k, n); return 0; } else { for(int i = 1; i <= n - 2*t; i++) { for(int j = i + 2*t; j <= n; j++) { temp = - (tab2[j] - tab2[j - t]) - (tab2[i + t - 1] - tab2[i - 1]) + tab1[j - t] - tab1[i + t - 1]; if(temp >= tab1[n] - k) { res = max(res, tab1[n] - (tab1[j] - tab1[i - 1]) + tab3[n] - (tab3[j] - tab3[i - 1]) + min(temp - (tab1[n] - k), tab2[n] - (tab2[j] - tab2[i - 1]))); } } } cout<<res; } } |
English