#include <iostream> #include <vector> #include <set> #include <map> #include <algorithm> #include <unordered_map> #include <unordered_set> #include <queue> #include <deque> #include <tuple> #include <bitset> using namespace std; #define pb push_back #define mp make_pair #define st first #define nd second #define x first #define y second typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, k, t; cin >> n >> k >> t; string s; cin >> s; vi D; vi B; vi Z; D.pb(0); B.pb(0); Z.pb(0); for(auto c: s){ int c_ = (int)c - (int)'0'; if(c_==1){ Z.pb(Z.back()); B.pb(B.back()+1); D.pb(D.back()); } if(c_==2){ Z.pb(Z.back()); B.pb(B.back()); D.pb(D.back()+1); } if(c_==3){ Z.pb(Z.back()+1); B.pb(B.back()); D.pb(D.back()); } } if(B.back()<=k){ cout << n-(max(D.back()-(k-B.back()), 0)) << "\n"; return 0; } int ans = -1; for(int i=0; i<=n-2*t; i++){ // Wyjeżdża o i for(int j=i+t; j<=n-t; j++){ int to_office = B[i+t] - B[i] + D[i+t] - D[i]; int back_home = B[j+t] - B[j] + D[j+t] - D[j]; int home_from_home = D[i] - D[0] + D[n] - D[j+t]; int office_from_home = B[i] - B[0] + B[n] - B[j+t]; int free_from_home = Z[i] - Z[0] + Z[n] - Z[j+t]; int sure_leave = to_office + back_home + office_from_home; if(sure_leave<=k){ ans = max(ans, free_from_home + office_from_home + min(k-sure_leave, home_from_home)); } } } cout << ans << "\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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | #include <iostream> #include <vector> #include <set> #include <map> #include <algorithm> #include <unordered_map> #include <unordered_set> #include <queue> #include <deque> #include <tuple> #include <bitset> using namespace std; #define pb push_back #define mp make_pair #define st first #define nd second #define x first #define y second typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, k, t; cin >> n >> k >> t; string s; cin >> s; vi D; vi B; vi Z; D.pb(0); B.pb(0); Z.pb(0); for(auto c: s){ int c_ = (int)c - (int)'0'; if(c_==1){ Z.pb(Z.back()); B.pb(B.back()+1); D.pb(D.back()); } if(c_==2){ Z.pb(Z.back()); B.pb(B.back()); D.pb(D.back()+1); } if(c_==3){ Z.pb(Z.back()+1); B.pb(B.back()); D.pb(D.back()); } } if(B.back()<=k){ cout << n-(max(D.back()-(k-B.back()), 0)) << "\n"; return 0; } int ans = -1; for(int i=0; i<=n-2*t; i++){ // Wyjeżdża o i for(int j=i+t; j<=n-t; j++){ int to_office = B[i+t] - B[i] + D[i+t] - D[i]; int back_home = B[j+t] - B[j] + D[j+t] - D[j]; int home_from_home = D[i] - D[0] + D[n] - D[j+t]; int office_from_home = B[i] - B[0] + B[n] - B[j+t]; int free_from_home = Z[i] - Z[0] + Z[n] - Z[j+t]; int sure_leave = to_office + back_home + office_from_home; if(sure_leave<=k){ ans = max(ans, free_from_home + office_from_home + min(k-sure_leave, home_from_home)); } } } cout << ans << "\n"; return 0; } |