#include <iostream> #include <cstdio> #include <vector> using namespace std; const int MX = 8005; char S[MX]; int L1[MX]; int L2[MX]; int main() { int n,k,t; cin>>n>>k>>t; cin >> S; for (int i=1;i<=n;i++) { L1[i]=L1[i-1]; L2[i]=L2[i-1]; if (S[i-1]=='1')L1[i]++; if (S[i-1]=='2')L2[i]++; } if (L1[n]<=k) { int a = L1[n]+L2[n]-k; cout << n - (a >=0 ? a : 0) << endl; return 0; } int res=-1; for (int i=1;i+2*t<=n;i++) { for (int j=i+t+1;j+t-1<=n;j++) { int mss = (L1[i+t-1]-L1[i-1]) + (L1[j+t-1]-L1[j-1]) + (L2[i+t-1]-L2[i-1]) + (L2[j+t-1]-L2[j-1]); int rof1 = L1[i-1] + L1[n]-L1[j+t-1]; int rof2 = L2[i-1] + L2[n]-L2[j+t-1]; int atm = k - (mss + rof1); if (atm < 0) continue; int t2ts = rof2 >= atm ? rof2-atm : 0; res = max(res, n -(j+t-i)-t2ts); } } cout << res << endl; 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 | #include <iostream> #include <cstdio> #include <vector> using namespace std; const int MX = 8005; char S[MX]; int L1[MX]; int L2[MX]; int main() { int n,k,t; cin>>n>>k>>t; cin >> S; for (int i=1;i<=n;i++) { L1[i]=L1[i-1]; L2[i]=L2[i-1]; if (S[i-1]=='1')L1[i]++; if (S[i-1]=='2')L2[i]++; } if (L1[n]<=k) { int a = L1[n]+L2[n]-k; cout << n - (a >=0 ? a : 0) << endl; return 0; } int res=-1; for (int i=1;i+2*t<=n;i++) { for (int j=i+t+1;j+t-1<=n;j++) { int mss = (L1[i+t-1]-L1[i-1]) + (L1[j+t-1]-L1[j-1]) + (L2[i+t-1]-L2[i-1]) + (L2[j+t-1]-L2[j-1]); int rof1 = L1[i-1] + L1[n]-L1[j+t-1]; int rof2 = L2[i-1] + L2[n]-L2[j+t-1]; int atm = k - (mss + rof1); if (atm < 0) continue; int t2ts = rof2 >= atm ? rof2-atm : 0; res = max(res, n -(j+t-i)-t2ts); } } cout << res << endl; return 0; } |