#include <bits/stdc++.h>
#define ull unsigned long long
#define pb push_back
#define pf push_front
#define in insert
#define f first
#define s second
#define pii pair<int, int>
#define pib pair<int, bool>
#define cint const int
#define mn2 262144
#define nw '\n'
#define sp " "
#define mn 8009
#define inf 400009
#define int long long
using namespace std;
int n, k, t, tab[mn], dop, pld[mn], plp[mn], pls[mn], pdep[mn];
int worm(){
bool wyw=true;
int wyn=-1;
for(int r=1+t; r+t-1<=n; ++r){
int rp=0, rd=0, rs=0, lp=0, ld=0, ls=0, dep=0, ret=0;
for(int x=r; x<r+t; ++x) if(tab[x]==1 || tab[x]==-1) ++ret;
for(int x=r+t; x<=n; ++x){
if(tab[x]==1) ++rd;
if(tab[x]==0) ++rp;
if(tab[x]==-1) ++rs;
}
for(int l=1; l<=r-t; ++l){
dep=0; lp=0; ls=0; ld=0;
dep=pdep[l+t-1]-pdep[l-1];
//for(int x=l; x<l+t; ++x) if(tab[x]==1 || tab[x]==-1) ++dep;
ld=pld[l-1];
lp=plp[l-1];
ls=pls[l-1];
// for(int x=1; x<l; ++x){
// if(tab[x]==1) ++ld;
// if(tab[x]==0) ++lp;
// if(tab[x]==-1) ++ls;
// }
int skip=dep+ret+rs+ls;
if(skip>k) continue;
wyw=false;
wyn=max(lp+ls+rp+rs+min(k-skip, ld+rd), wyn);
}
}
if(wyw) return -1;
else return wyn;
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>n>>k>>t;
char a;
int ob=0, zdal=0, pot=0;
for(int i=1; i<=n; ++i){
cin>>a;
if(a=='2'){ tab[i]=1; ++zdal; ++pld[i]; }
if(a=='3'){ tab[i]=0; ++pot; ++plp[i]; }
if(a=='1'){ tab[i]=-1; ++ob; ++pls[i]; }
if(tab[i]==1 || tab[i]==-1) ++pdep[i];
pld[i]+=pld[i-1];
plp[i]+=plp[i-1];
pls[i]+=pls[i-1];
pdep[i]+=pdep[i-1];
}
int odp=worm();
if(ob<=k) odp=max(odp, ob+pot+min(k-ob, zdal));
if (odp<0) odp=-1;
cout<<odp;
}
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 61 62 63 64 65 66 67 68 69 70 71 | #include <bits/stdc++.h> #define ull unsigned long long #define pb push_back #define pf push_front #define in insert #define f first #define s second #define pii pair<int, int> #define pib pair<int, bool> #define cint const int #define mn2 262144 #define nw '\n' #define sp " " #define mn 8009 #define inf 400009 #define int long long using namespace std; int n, k, t, tab[mn], dop, pld[mn], plp[mn], pls[mn], pdep[mn]; int worm(){ bool wyw=true; int wyn=-1; for(int r=1+t; r+t-1<=n; ++r){ int rp=0, rd=0, rs=0, lp=0, ld=0, ls=0, dep=0, ret=0; for(int x=r; x<r+t; ++x) if(tab[x]==1 || tab[x]==-1) ++ret; for(int x=r+t; x<=n; ++x){ if(tab[x]==1) ++rd; if(tab[x]==0) ++rp; if(tab[x]==-1) ++rs; } for(int l=1; l<=r-t; ++l){ dep=0; lp=0; ls=0; ld=0; dep=pdep[l+t-1]-pdep[l-1]; //for(int x=l; x<l+t; ++x) if(tab[x]==1 || tab[x]==-1) ++dep; ld=pld[l-1]; lp=plp[l-1]; ls=pls[l-1]; // for(int x=1; x<l; ++x){ // if(tab[x]==1) ++ld; // if(tab[x]==0) ++lp; // if(tab[x]==-1) ++ls; // } int skip=dep+ret+rs+ls; if(skip>k) continue; wyw=false; wyn=max(lp+ls+rp+rs+min(k-skip, ld+rd), wyn); } } if(wyw) return -1; else return wyn; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>k>>t; char a; int ob=0, zdal=0, pot=0; for(int i=1; i<=n; ++i){ cin>>a; if(a=='2'){ tab[i]=1; ++zdal; ++pld[i]; } if(a=='3'){ tab[i]=0; ++pot; ++plp[i]; } if(a=='1'){ tab[i]=-1; ++ob; ++pls[i]; } if(tab[i]==1 || tab[i]==-1) ++pdep[i]; pld[i]+=pld[i-1]; plp[i]+=plp[i-1]; pls[i]+=pls[i-1]; pdep[i]+=pdep[i-1]; } int odp=worm(); if(ob<=k) odp=max(odp, ob+pot+min(k-ob, zdal)); if (odp<0) odp=-1; cout<<odp; } |
English