#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n,k,t; cin>>n>>k>>t;
string s; cin>>s;s=" "+s;
vector<int>P1(n+1),P2(n+1),P3(n+1);
P1[0]=0;P2[0]=0;P3[0]=0;
for(int i=1;i<=n;i++){
if(s[i]=='1'){
P1[i]=P1[i-1]+1;
P2[i]=P2[i-1];
P3[i]=P3[i-1];
}
else if(s[i]=='2'){
P1[i]=P1[i-1];
P2[i]=P2[i-1]+1;
P3[i]=P3[i-1];
}
else{
P1[i]=P1[i-1];
P2[i]=P2[i-1];
P3[i]=P3[i-1]+1;
}
}
int wyn=-1;
if(P1[n]<=k)wyn=max(wyn,P3[n]+P1[n]+min(P2[n],k-P1[n]));
int pom1,pom2;
for(int i=1;i<=n-2*t+1;i++){
for(int j=i+t;j<=n-t+1;j++){
pom1=P1[i+t-1]-P1[i-1]+P2[i+t-1]-P2[i-1]+P1[j+t-1]-P1[j-1]+P2[j+t-1]-P2[j-1];
if(pom1>k)continue;
pom2=P1[i-1]+(P1[n]-P1[j+t-1]);
if(pom2+pom1>k)continue;
wyn=max(wyn,pom2+P3[i-1]+P3[n]-P3[j+t-1]+min(P2[i-1]+P2[n]-P2[j+t-1],k-pom1-P1[i-1]-P1[n]+P1[j+t-1]));
}
}
cout<<wyn;
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 | #include <bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,k,t; cin>>n>>k>>t; string s; cin>>s;s=" "+s; vector<int>P1(n+1),P2(n+1),P3(n+1); P1[0]=0;P2[0]=0;P3[0]=0; for(int i=1;i<=n;i++){ if(s[i]=='1'){ P1[i]=P1[i-1]+1; P2[i]=P2[i-1]; P3[i]=P3[i-1]; } else if(s[i]=='2'){ P1[i]=P1[i-1]; P2[i]=P2[i-1]+1; P3[i]=P3[i-1]; } else{ P1[i]=P1[i-1]; P2[i]=P2[i-1]; P3[i]=P3[i-1]+1; } } int wyn=-1; if(P1[n]<=k)wyn=max(wyn,P3[n]+P1[n]+min(P2[n],k-P1[n])); int pom1,pom2; for(int i=1;i<=n-2*t+1;i++){ for(int j=i+t;j<=n-t+1;j++){ pom1=P1[i+t-1]-P1[i-1]+P2[i+t-1]-P2[i-1]+P1[j+t-1]-P1[j-1]+P2[j+t-1]-P2[j-1]; if(pom1>k)continue; pom2=P1[i-1]+(P1[n]-P1[j+t-1]); if(pom2+pom1>k)continue; wyn=max(wyn,pom2+P3[i-1]+P3[n]-P3[j+t-1]+min(P2[i-1]+P2[n]-P2[j+t-1],k-pom1-P1[i-1]-P1[n]+P1[j+t-1])); } } cout<<wyn; return 0; } |
English