#include <bits/stdc++.h> #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define st first #define nd second using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " using ll = long long; using pii = pair<int,int>; using pll = pair<ll,ll>; using vi = vector<int>; using vll = vector<ll>; template <typename T> void mini(T &a, T b) { a = min(a, b); } const ll kInf = 1e18; inline ll DivFloor(ll a, ll b) { assert(b > 0); ll res = a / b; if (res * b > a) { --res; } return res; } struct DecSeq { ll cur_delta = 0; int num_elems = 0; int cur_delta_ptr = 0; struct LineInfo { ll start; pll line; bool operator<(const LineInfo &L) const { return start < L.start; } ll Eval(ll x) const { return line.st * x + line.nd; } }; vector<LineInfo> lines; void AddLine(const pll &L) { while (!lines.empty()) { auto &P = lines[SZ(lines) - 1]; const ll when_takeover = DivFloor(L.nd - P.line.nd, P.line.st - L.st) + 1; if (when_takeover <= P.start) { lines.pop_back(); } else { lines.push_back({when_takeover, L}); if (when_takeover <= cur_delta) { cur_delta_ptr = SZ(lines) - 1; } return; } } cur_delta_ptr = 0; lines.push_back({-kInf, L}); } void AddElem(ll value) { value += (ll)cur_delta * num_elems; pll new_line{-num_elems, value}; AddLine(new_line); ++num_elems; } void DecreaseAll(ll amount) { cur_delta += amount; while (cur_delta_ptr + 1 < SZ(lines) && cur_delta >= lines[cur_delta_ptr + 1].start) { ++cur_delta_ptr; } } pll GetOpt() { auto &line = lines[cur_delta_ptr]; return pll{line.Eval(cur_delta), -line.line.st}; } friend ostream &operator<<(ostream &os, const DecSeq &ds) { os << "{["; for (auto line : ds.lines) { os << "(" << line.line.st << "," << line.line.nd << ")"; } return os << "] delta=" << ds.cur_delta << "}"; } }; string str; int n, k; pll SolveWithPenaltyPerIntv(ll penalty_per_intv) { const ll mul_coef = n + 1; penalty_per_intv = penalty_per_intv * mul_coef + 1; //~ debug() << "real penalty" << imie(penalty_per_intv); vll dp(n + 1); ll global_delta = 0; vector<DecSeq> seqs(1); vll mins_stack; dp[0] = penalty_per_intv; seqs[0].AddElem(penalty_per_intv); mins_stack.push_back(penalty_per_intv); //~ debug() << "at start"; //~ debug() << imie(mins_stack) << imie(global_delta); //~ debug() << imie(seqs); for (int i = 0; i < n; ++i) { if (str[i] == '(') { seqs.emplace_back(); const ll elem = dp[i] + penalty_per_intv - global_delta; seqs.back().AddElem(elem); mins_stack.emplace_back(min(mins_stack.back(), elem)); } else { assert(!mins_stack.empty()); const ll best_top = seqs.back().GetOpt().st; seqs.pop_back(); mins_stack.pop_back(); if (seqs.empty()) { seqs.emplace_back(); seqs[0].AddElem(best_top); mins_stack.push_back(best_top); } else { const int cnt_at_level = seqs.back().num_elems; seqs.back().AddElem(best_top); global_delta += (ll)cnt_at_level * mul_coef; seqs.back().DecreaseAll(mul_coef); mins_stack.back() = seqs.back().GetOpt().st; if (SZ(mins_stack) >= 2) { mini(mins_stack.back(), mins_stack[SZ(mins_stack) - 2]); } } } //~ debug() << imie(i) << imie(str[i]) << imie(mins_stack) << imie(global_delta); //~ debug() << imie(seqs); dp[i + 1] = mins_stack.back() + global_delta; //~ debug() << imie(dp[i + 1]); } const ll ans = dp.back(); return {ans / mul_coef, ans % mul_coef}; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(11); cerr << fixed << setprecision(6); cin >> n >> k >> str; ll min_coef = -1, max_coef = 2e10; ll ans = -1e18; while (max_coef - min_coef > 1) { const ll mid_coef = (min_coef + max_coef) / 2; auto penalty_sol = SolveWithPenaltyPerIntv(mid_coef); debug() << imie(mid_coef) << imie(penalty_sol); if (penalty_sol.nd <= k) { ll cand = penalty_sol.st - k * mid_coef; ans = max(ans, cand); max_coef = mid_coef; } else { min_coef = mid_coef; } } cout << ans << "\n"; }
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | #include <bits/stdc++.h> #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define st first #define nd second using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge { c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " using ll = long long; using pii = pair<int,int>; using pll = pair<ll,ll>; using vi = vector<int>; using vll = vector<ll>; template <typename T> void mini(T &a, T b) { a = min(a, b); } const ll kInf = 1e18; inline ll DivFloor(ll a, ll b) { assert(b > 0); ll res = a / b; if (res * b > a) { --res; } return res; } struct DecSeq { ll cur_delta = 0; int num_elems = 0; int cur_delta_ptr = 0; struct LineInfo { ll start; pll line; bool operator<(const LineInfo &L) const { return start < L.start; } ll Eval(ll x) const { return line.st * x + line.nd; } }; vector<LineInfo> lines; void AddLine(const pll &L) { while (!lines.empty()) { auto &P = lines[SZ(lines) - 1]; const ll when_takeover = DivFloor(L.nd - P.line.nd, P.line.st - L.st) + 1; if (when_takeover <= P.start) { lines.pop_back(); } else { lines.push_back({when_takeover, L}); if (when_takeover <= cur_delta) { cur_delta_ptr = SZ(lines) - 1; } return; } } cur_delta_ptr = 0; lines.push_back({-kInf, L}); } void AddElem(ll value) { value += (ll)cur_delta * num_elems; pll new_line{-num_elems, value}; AddLine(new_line); ++num_elems; } void DecreaseAll(ll amount) { cur_delta += amount; while (cur_delta_ptr + 1 < SZ(lines) && cur_delta >= lines[cur_delta_ptr + 1].start) { ++cur_delta_ptr; } } pll GetOpt() { auto &line = lines[cur_delta_ptr]; return pll{line.Eval(cur_delta), -line.line.st}; } friend ostream &operator<<(ostream &os, const DecSeq &ds) { os << "{["; for (auto line : ds.lines) { os << "(" << line.line.st << "," << line.line.nd << ")"; } return os << "] delta=" << ds.cur_delta << "}"; } }; string str; int n, k; pll SolveWithPenaltyPerIntv(ll penalty_per_intv) { const ll mul_coef = n + 1; penalty_per_intv = penalty_per_intv * mul_coef + 1; //~ debug() << "real penalty" << imie(penalty_per_intv); vll dp(n + 1); ll global_delta = 0; vector<DecSeq> seqs(1); vll mins_stack; dp[0] = penalty_per_intv; seqs[0].AddElem(penalty_per_intv); mins_stack.push_back(penalty_per_intv); //~ debug() << "at start"; //~ debug() << imie(mins_stack) << imie(global_delta); //~ debug() << imie(seqs); for (int i = 0; i < n; ++i) { if (str[i] == '(') { seqs.emplace_back(); const ll elem = dp[i] + penalty_per_intv - global_delta; seqs.back().AddElem(elem); mins_stack.emplace_back(min(mins_stack.back(), elem)); } else { assert(!mins_stack.empty()); const ll best_top = seqs.back().GetOpt().st; seqs.pop_back(); mins_stack.pop_back(); if (seqs.empty()) { seqs.emplace_back(); seqs[0].AddElem(best_top); mins_stack.push_back(best_top); } else { const int cnt_at_level = seqs.back().num_elems; seqs.back().AddElem(best_top); global_delta += (ll)cnt_at_level * mul_coef; seqs.back().DecreaseAll(mul_coef); mins_stack.back() = seqs.back().GetOpt().st; if (SZ(mins_stack) >= 2) { mini(mins_stack.back(), mins_stack[SZ(mins_stack) - 2]); } } } //~ debug() << imie(i) << imie(str[i]) << imie(mins_stack) << imie(global_delta); //~ debug() << imie(seqs); dp[i + 1] = mins_stack.back() + global_delta; //~ debug() << imie(dp[i + 1]); } const ll ans = dp.back(); return {ans / mul_coef, ans % mul_coef}; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(11); cerr << fixed << setprecision(6); cin >> n >> k >> str; ll min_coef = -1, max_coef = 2e10; ll ans = -1e18; while (max_coef - min_coef > 1) { const ll mid_coef = (min_coef + max_coef) / 2; auto penalty_sol = SolveWithPenaltyPerIntv(mid_coef); debug() << imie(mid_coef) << imie(penalty_sol); if (penalty_sol.nd <= k) { ll cand = penalty_sol.st - k * mid_coef; ans = max(ans, cand); max_coef = mid_coef; } else { min_coef = mid_coef; } } cout << ans << "\n"; } |