#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
constexpr int N = 8000;
int pref[N+9][3];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n,k,t;
string a;
cin >> n >> k >> t;
cin >> a;
for (int x=1;x<=n;x++){
pref[x][0]=pref[x-1][0]; pref[x][1]=pref[x-1][1]; pref[x][2]=pref[x-1][2];
pref[x][a[x-1]-'1']++;
}
k = pref[n][0]+pref[n][1]-k;
int odp=-1;
for (int x=t+1;x<=n;x++){
for (int y=x;y<=n-t;y++){
int biur = pref[y][0]-pref[x-1][0] + pref[y][1]-pref[x-1][1];
int free = x-t + n-t-y-1;
int zdal = pref[x-t][1] + pref[n][1]-pref[y+t][1];
if (biur + zdal >= k){
odp=max(odp,free-max(k-biur,0));
}
}
}
if (pref[n][1] >= k)odp=max(odp,n-max(k,0));
cout << odp << '\n';
}
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 | #include <bits/stdc++.h> using namespace std; typedef long long int ll; constexpr int N = 8000; int pref[N+9][3]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,k,t; string a; cin >> n >> k >> t; cin >> a; for (int x=1;x<=n;x++){ pref[x][0]=pref[x-1][0]; pref[x][1]=pref[x-1][1]; pref[x][2]=pref[x-1][2]; pref[x][a[x-1]-'1']++; } k = pref[n][0]+pref[n][1]-k; int odp=-1; for (int x=t+1;x<=n;x++){ for (int y=x;y<=n-t;y++){ int biur = pref[y][0]-pref[x-1][0] + pref[y][1]-pref[x-1][1]; int free = x-t + n-t-y-1; int zdal = pref[x-t][1] + pref[n][1]-pref[y+t][1]; if (biur + zdal >= k){ odp=max(odp,free-max(k-biur,0)); } } } if (pref[n][1] >= k)odp=max(odp,n-max(k,0)); cout << odp << '\n'; } |
English