#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, k, t;
cin >> n >> k >> t;
string s;
cin >> s;
s = '3' + s;
int px1[n+1], px2[n+1], px3[n+1];
px1[0] = 0;
px2[0] = 0;
px3[0] = 0;
for(int i = 1; i <= n; i++){
px1[i] = px1[i-1];
px2[i] = px2[i-1];
px3[i] = px3[i-1];
if(s[i] == '1') px1[i]++;
else if(s[i] == '2') px2[i]++;
else px3[i]++;
if(s[i] != '1') continue;
if(i > t & i + t <= n) continue;
cout << -1;
return 0;
}
if(px1[n] <= k){
cout << min(px3[n] + k, n);
return 0;
}
int maxGoHome = n - t;
int maxResult = 0;
int len;
int k1 = k;
for(int i = t+1; i <= maxGoHome; i++){
for(int j = i; j <= maxGoHome; j++){
len = px1[i-1] + px1[n] - px1[j] + px2[i-1] - px2[i-t-1] + px2[j+t] - px2[j];
if(len > k) continue;
k1 = k;
k1 -= (px1[i-1] - px1[i-t-1] + px1[j+t] - px1[j] + px2[i-1] - px2[i-t-1] + px2[j+t] - px2[j]);
len = min(px3[i-t-1] + px3[n] - px3[j+t] + k1, n - j + i - 1);
maxResult = max(maxResult, len);
break;
}
}
cout << maxResult;
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 | #include <bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k, t; cin >> n >> k >> t; string s; cin >> s; s = '3' + s; int px1[n+1], px2[n+1], px3[n+1]; px1[0] = 0; px2[0] = 0; px3[0] = 0; for(int i = 1; i <= n; i++){ px1[i] = px1[i-1]; px2[i] = px2[i-1]; px3[i] = px3[i-1]; if(s[i] == '1') px1[i]++; else if(s[i] == '2') px2[i]++; else px3[i]++; if(s[i] != '1') continue; if(i > t & i + t <= n) continue; cout << -1; return 0; } if(px1[n] <= k){ cout << min(px3[n] + k, n); return 0; } int maxGoHome = n - t; int maxResult = 0; int len; int k1 = k; for(int i = t+1; i <= maxGoHome; i++){ for(int j = i; j <= maxGoHome; j++){ len = px1[i-1] + px1[n] - px1[j] + px2[i-1] - px2[i-t-1] + px2[j+t] - px2[j]; if(len > k) continue; k1 = k; k1 -= (px1[i-1] - px1[i-t-1] + px1[j+t] - px1[j] + px2[i-1] - px2[i-t-1] + px2[j+t] - px2[j]); len = min(px3[i-t-1] + px3[n] - px3[j+t] + k1, n - j + i - 1); maxResult = max(maxResult, len); break; } } cout << maxResult; return 0; } |
English