#include<bits/stdc++.h>
using namespace std;
const int MAXN = 8e3 + 7;
int biuro[MAXN];
int zdalne[MAXN];
int wolne[MAXN];
int prz(int l, int r, int tab[MAXN]){
return tab[r] - tab[l];
}
int spr(int k, int pocz, int t, int kon, int n){
return prz(pocz - 1, pocz + t - 1, zdalne) + prz(0, pocz + t - 1, biuro) +
prz(kon - 1, kon + t - 1, zdalne) + prz(kon - 1, n, biuro);
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n, k, t;
cin >> n >> k >> t;
char c;
for(int i = 1; i <= n; i++){
cin >> c;
biuro[i] = biuro[i - 1];
zdalne[i] = zdalne[i - 1];
wolne[i] = wolne[i - 1];
if(c == '1') biuro[i]++;
else if(c == '2') zdalne[i]++;
else wolne[i]++;
}
int ans = -1;
if(biuro[n] <= k){
ans = n - max(zdalne[n] - k + biuro[n], 0);
}
// cerr << "ans: " << ans << '\n';
for(int i = 1; i <= n - 2 * t; i++){
// cerr << "i: " << i << '\n';
for(int j = i + t; j <= n - t + 1; j++){
// cerr << "j: " << j << '\n';
int a = spr(k, i, t, j, n);
// cerr << "a: " << a << '\n';
if(a <= k){
// cerr << "ok\n";
int godz = wolne[n] - prz(i - 1, j + t - 1, wolne) + biuro[n] - prz(i - 1, j + t - 1, biuro);
godz += min(k - a, zdalne[n] - prz(i - 1, j + t - 1, zdalne));
ans = max(ans, godz);
// cerr << "ans: " << ans << '\n';
}
}
}
cout << ans << '\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 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; const int MAXN = 8e3 + 7; int biuro[MAXN]; int zdalne[MAXN]; int wolne[MAXN]; int prz(int l, int r, int tab[MAXN]){ return tab[r] - tab[l]; } int spr(int k, int pocz, int t, int kon, int n){ return prz(pocz - 1, pocz + t - 1, zdalne) + prz(0, pocz + t - 1, biuro) + prz(kon - 1, kon + t - 1, zdalne) + prz(kon - 1, n, biuro); } int main(){ ios_base::sync_with_stdio(0);cin.tie(0); int n, k, t; cin >> n >> k >> t; char c; for(int i = 1; i <= n; i++){ cin >> c; biuro[i] = biuro[i - 1]; zdalne[i] = zdalne[i - 1]; wolne[i] = wolne[i - 1]; if(c == '1') biuro[i]++; else if(c == '2') zdalne[i]++; else wolne[i]++; } int ans = -1; if(biuro[n] <= k){ ans = n - max(zdalne[n] - k + biuro[n], 0); } // cerr << "ans: " << ans << '\n'; for(int i = 1; i <= n - 2 * t; i++){ // cerr << "i: " << i << '\n'; for(int j = i + t; j <= n - t + 1; j++){ // cerr << "j: " << j << '\n'; int a = spr(k, i, t, j, n); // cerr << "a: " << a << '\n'; if(a <= k){ // cerr << "ok\n"; int godz = wolne[n] - prz(i - 1, j + t - 1, wolne) + biuro[n] - prz(i - 1, j + t - 1, biuro); godz += min(k - a, zdalne[n] - prz(i - 1, j + t - 1, zdalne)); ans = max(ans, godz); // cerr << "ans: " << ans << '\n'; } } } cout << ans << '\n'; } |
English