#include<bits/stdc++.h> using namespace std; int spot[8002], ospot[8002], biuro, wyn=-1; int main() { ios_base::sync_with_stdio(false); int n, k, t; cin>>n>>k>>t; for(int i=1; i<=n; ++i) { char c; cin>>c; if(c=='1' || c=='2') spot[i]=spot[i-1]+1; else spot[i]=spot[i-1]; if(c=='2') ospot[i]=ospot[i-1]+1; else ospot[i]=ospot[i-1]; if(c=='1') biuro++; } int iletrzeba = max(0,spot[n]-k); //cout<<iletrzeba<<endl; if(biuro <= k) { cout<<n-iletrzeba<<"\n"; return 0; } for(int i=1; i<=n-2*t; ++i) { for(int j=i+t+1; j<=n-t+1; ++j) { int wynik=0; int ilezal = spot[j-1] - spot[i+t-1]; int iledost = ospot[i-1] + ospot[n]- ospot[j+t-1]; if(iletrzeba - ilezal - iledost > 0) { wynik = -1; wyn=max(wyn, wynik); continue; } int ilewdomu = i-1 + n-(j+t-1); int iletrzebawdomu = iletrzeba - ilezal; if(iletrzebawdomu > 0) wynik = ilewdomu - iletrzebawdomu; else wynik = ilewdomu; wyn=max(wyn, wynik); } } cout<<wyn<<"\n"; 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 | #include<bits/stdc++.h> using namespace std; int spot[8002], ospot[8002], biuro, wyn=-1; int main() { ios_base::sync_with_stdio(false); int n, k, t; cin>>n>>k>>t; for(int i=1; i<=n; ++i) { char c; cin>>c; if(c=='1' || c=='2') spot[i]=spot[i-1]+1; else spot[i]=spot[i-1]; if(c=='2') ospot[i]=ospot[i-1]+1; else ospot[i]=ospot[i-1]; if(c=='1') biuro++; } int iletrzeba = max(0,spot[n]-k); //cout<<iletrzeba<<endl; if(biuro <= k) { cout<<n-iletrzeba<<"\n"; return 0; } for(int i=1; i<=n-2*t; ++i) { for(int j=i+t+1; j<=n-t+1; ++j) { int wynik=0; int ilezal = spot[j-1] - spot[i+t-1]; int iledost = ospot[i-1] + ospot[n]- ospot[j+t-1]; if(iletrzeba - ilezal - iledost > 0) { wynik = -1; wyn=max(wyn, wynik); continue; } int ilewdomu = i-1 + n-(j+t-1); int iletrzebawdomu = iletrzeba - ilezal; if(iletrzebawdomu > 0) wynik = ilewdomu - iletrzebawdomu; else wynik = ilewdomu; wyn=max(wyn, wynik); } } cout<<wyn<<"\n"; return 0; } |