#include<bits/stdc++.h> using namespace std; void solve() { int a,k,t; cin>>a>>k>>t; vector<int> preW(a+1,0); vector<int> preZ(a+1,0); vector<int> preS(a+1,0); string s; cin>>s; for(int z=1;z<=a;z++) { preW[z]=preW[z-1]; preZ[z]=preZ[z-1]; preS[z]=preS[z-1]; if(s[z-1]=='1')preS[z]++; if(s[z-1]=='2')preZ[z]++; if(s[z-1]=='3')preW[z]++; } int wyn=-1; int pom=preS[a]; if(pom<=k) { if(pom+preZ[a]<=k)wyn=a;else wyn=preW[a]+k; } for(int i=1;i<=a;i++) { for(int j=i;j<=a;j++) { if(i-t<1||j+t>a)continue; int omi=preZ[i-1]-preZ[i-t-1]+preS[i-1]-preS[i-t-1]; omi+=preZ[j+t]-preZ[j]+preS[j+t]-preS[j]; omi+=preS[i-t-1]+preS[a]-preS[j+t]; if(omi>k)continue; int wol=preW[i-t-1]+preW[a]-preW[j+t]+preS[i-t-1]+preS[a]-preS[j+t]; int spo=preZ[i-t-1]+preZ[a]-preZ[j+t]; if(spo+omi<=k) { wyn=max(wyn,wol+spo); }else { wyn=max(wyn,wol+k-omi); } } } cout<<wyn<<"\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int h=1; while(h--)solve(); 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | #include<bits/stdc++.h> using namespace std; void solve() { int a,k,t; cin>>a>>k>>t; vector<int> preW(a+1,0); vector<int> preZ(a+1,0); vector<int> preS(a+1,0); string s; cin>>s; for(int z=1;z<=a;z++) { preW[z]=preW[z-1]; preZ[z]=preZ[z-1]; preS[z]=preS[z-1]; if(s[z-1]=='1')preS[z]++; if(s[z-1]=='2')preZ[z]++; if(s[z-1]=='3')preW[z]++; } int wyn=-1; int pom=preS[a]; if(pom<=k) { if(pom+preZ[a]<=k)wyn=a;else wyn=preW[a]+k; } for(int i=1;i<=a;i++) { for(int j=i;j<=a;j++) { if(i-t<1||j+t>a)continue; int omi=preZ[i-1]-preZ[i-t-1]+preS[i-1]-preS[i-t-1]; omi+=preZ[j+t]-preZ[j]+preS[j+t]-preS[j]; omi+=preS[i-t-1]+preS[a]-preS[j+t]; if(omi>k)continue; int wol=preW[i-t-1]+preW[a]-preW[j+t]+preS[i-t-1]+preS[a]-preS[j+t]; int spo=preZ[i-t-1]+preZ[a]-preZ[j+t]; if(spo+omi<=k) { wyn=max(wyn,wol+spo); }else { wyn=max(wyn,wol+k-omi); } } } cout<<wyn<<"\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int h=1; while(h--)solve(); return 0; } |