#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define imie(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n' using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; const int mod=1e9+7; typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; vector<int> pref1, pref2, pref3; int koszt(const vector<int> &pref, int l, int r) { return pref[r] - pref[l]; } int koszt_przedzial(int l, int r) { return koszt(pref1, l, r) + koszt(pref2, l, r); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k, t; cin >> n >> k >> t; string s; cin >> s; pref1.assign(n + 1, 0); pref2.assign(n + 1, 0); pref3.assign(n + 1, 0); for(int i = 0; i < n; i++) { pref1[i + 1] = pref1[i] + (s[i] == '1'); pref2[i + 1] = pref2[i] + (s[i] == '2'); pref3[i + 1] = pref3[i] + (s[i] == '3'); } int ans = -1; int t1 = koszt(pref1, 0, n); int t2 = koszt(pref2, 0, n); int t3 = koszt(pref3, 0, n); if(t1 <= k) { int dod = min(t2, k - t1); ans = max(ans, t3 + t1 + dod); } for(int i = 0; i <= n; i++) { if(i + t > n) break; int c1 = koszt(pref1, 0, i); int c2 = koszt(pref2, 0, i); int c3 = koszt(pref3, 0, i); for(int j = i + 2 * t; j <= n; j++) { int k1 = koszt_przedzial(i, i + t); int k2 = koszt_przedzial(j - t, j); int sum = k1 + k2; int h = k - sum; if(h < 0) continue; int r1 = koszt(pref1, j, n); int r2 = koszt(pref2, j, n); int r3 = koszt(pref3, j, n); if(h < c1 + r1) continue; int ml = c1 + c2; int mr = r1 + r2; int mt = ml + mr; int mg = min(h, mt); int mrr = c3 + r3 + mg; ans = max(ans, mrr); } } cout << ans; 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> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define imie(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n' using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; const int mod=1e9+7; typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set; vector<int> pref1, pref2, pref3; int koszt(const vector<int> &pref, int l, int r) { return pref[r] - pref[l]; } int koszt_przedzial(int l, int r) { return koszt(pref1, l, r) + koszt(pref2, l, r); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k, t; cin >> n >> k >> t; string s; cin >> s; pref1.assign(n + 1, 0); pref2.assign(n + 1, 0); pref3.assign(n + 1, 0); for(int i = 0; i < n; i++) { pref1[i + 1] = pref1[i] + (s[i] == '1'); pref2[i + 1] = pref2[i] + (s[i] == '2'); pref3[i + 1] = pref3[i] + (s[i] == '3'); } int ans = -1; int t1 = koszt(pref1, 0, n); int t2 = koszt(pref2, 0, n); int t3 = koszt(pref3, 0, n); if(t1 <= k) { int dod = min(t2, k - t1); ans = max(ans, t3 + t1 + dod); } for(int i = 0; i <= n; i++) { if(i + t > n) break; int c1 = koszt(pref1, 0, i); int c2 = koszt(pref2, 0, i); int c3 = koszt(pref3, 0, i); for(int j = i + 2 * t; j <= n; j++) { int k1 = koszt_przedzial(i, i + t); int k2 = koszt_przedzial(j - t, j); int sum = k1 + k2; int h = k - sum; if(h < 0) continue; int r1 = koszt(pref1, j, n); int r2 = koszt(pref2, j, n); int r3 = koszt(pref3, j, n); if(h < c1 + r1) continue; int ml = c1 + c2; int mr = r1 + r2; int mt = ml + mr; int mg = min(h, mt); int mrr = c3 + r3 + mg; ans = max(ans, mrr); } } cout << ans; return 0; } |