#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k, t;
int odp=-1;
vector<int>A;
vector<int>SZ;
vector<int>SZD;
vector<int>S;
cin>>n>>k>>t;
for(int i=0; i<n; i++)
{
char a;
cin>>a;
A.push_back(a-'0');
}
SZ.push_back(0);
S.push_back(0);
SZD.push_back(0);
for(int i=0; i<n; i++)
{
S.push_back(S[i]);
SZ.push_back(SZ[i]);
SZD.push_back(SZD[i]);
if(A[i]==2)
{
S[i+1]++;
SZD[i+1]++;
}
if(A[i]==1)
{
S[i+1]++;
SZ[i+1]++;
}
}
SZ.push_back(SZ[n]);
SZD.push_back(SZD[n]);
S.push_back(S[n]);
//pierwszy indeks zero ostatni tyle ile poprzedni
if(SZ[n]<=k)
{
odp=n-max(0, S[n]-k);
}
for(int i=1; i<=n; i++)
{
//j to pierwszy indeks kiedy w domu
for(int j=i+2*t; j<=n+1; j++)
{
int il=k;
int w=S[i+t-1]-S[i-1];
w+=S[j-1]-S[j-t-1];
//nad wszystkie ktore w czasie trasy
w+=SZ[i-1];
w+=SZ[n]-SZ[j-1];il-=w;
if(il<0)
{
continue;
}
int h=SZD[i-1]+SZD[n]-SZD[j-1];
if(il>=h)
{
odp=max(odp, i+n-j);
}else
{
odp=max(odp, i+n-j+il-h);
}
}
}
cout<<odp;
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 71 72 73 74 75 76 | #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, k, t; int odp=-1; vector<int>A; vector<int>SZ; vector<int>SZD; vector<int>S; cin>>n>>k>>t; for(int i=0; i<n; i++) { char a; cin>>a; A.push_back(a-'0'); } SZ.push_back(0); S.push_back(0); SZD.push_back(0); for(int i=0; i<n; i++) { S.push_back(S[i]); SZ.push_back(SZ[i]); SZD.push_back(SZD[i]); if(A[i]==2) { S[i+1]++; SZD[i+1]++; } if(A[i]==1) { S[i+1]++; SZ[i+1]++; } } SZ.push_back(SZ[n]); SZD.push_back(SZD[n]); S.push_back(S[n]); //pierwszy indeks zero ostatni tyle ile poprzedni if(SZ[n]<=k) { odp=n-max(0, S[n]-k); } for(int i=1; i<=n; i++) { //j to pierwszy indeks kiedy w domu for(int j=i+2*t; j<=n+1; j++) { int il=k; int w=S[i+t-1]-S[i-1]; w+=S[j-1]-S[j-t-1]; //nad wszystkie ktore w czasie trasy w+=SZ[i-1]; w+=SZ[n]-SZ[j-1];il-=w; if(il<0) { continue; } int h=SZD[i-1]+SZD[n]-SZD[j-1]; if(il>=h) { odp=max(odp, i+n-j); }else { odp=max(odp, i+n-j+il-h); } } } cout<<odp; return 0; } |
English