#include <algorithm> #include <array> #include <cassert> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <ranges> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long LL; #define int LL typedef pair<int, int> PII; const int INF = 1e18; #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() int n, kk; string s; vector<vector<int>> cost; /* vector<vector<int>> memo_f; int f(int i, int j) { if (i < 0) return -INF; if (j < 0) return i == 0 ? 0 : -INF; int &res = memo_f[i][j]; if (res != -1) return res; res = INF; for (int k = 0; k <= j; k++) { int v = f(i - 1, k - 1); if (v == -INF) continue; res = min(res, cost[k][j] + f(i - 1, k - 1)); } return res; }*/ struct S { int l, r; bool broken; S(int _l, int _r, bool _broken = false) : l(_l), r(_r), broken(_broken){}; }; vector<vector<S>> lists; vector<vector<int>> match_memo; int match(int i, int sum) { if (i == s.size()) return INF; if (sum < 0) return INF; int &res = match_memo[i][sum]; if (res != -1) return res; sum += s[i] == '(' ? 1 : -1; if (sum == 0) return i; return res = match(i + 1, sum); } void parsed(vector<S> list) { if (!list.empty()) lists.push_back(list); } void parse(int l, int r) { assert(l < r); vector<S> list; while (true) { while (l < r && s[l] != '(') { list.push_back({S(l, l, true)}); l++; } if (l == r) break; assert(s[l] == '('); int j; for (j = l; j < r; j++) { if (s[j] == '(' && match(j, 0) < r) { while (l < j) { list.push_back({S(l, l, true)}); l++; } l = j; j = match(j, 0); break; } } if (j == r) break; assert(s[j] == ')'); list.push_back(S(l, j)); if (j != l + 1) parse(l + 1, j); l = j + 1; } parsed(list); } vector<vector<int>> dpf; void huj(int g, int i, int j, int l, int r) { if (i > j) return; int mid = (i + j) / 2; int best = l; for (int k = l; k <= min(r, mid); k++) { int val = dpf[g - 1][k - 1] + cost[k - 1][mid - 1]; if (val < dpf[g][mid]) { dpf[g][mid] = val; best = k; } } huj(g, i, mid - 1, l, best); huj(g, mid + 1, j, best, r); } int dupa(int k, int n) { n++; k++; dpf.resize(k + 1); for (auto &x : dpf) { x.resize(n + 1); for (auto &y : x) y = INF; } for (int i = 0; i <= k; i++) dpf[i][0] = i == 0; for (int j = 0; j <= n; j++) dpf[0][j] = INF; for (int i = 1; i <= k; i++) huj(i, 1, n, 1, n); return dpf[k][n]; } int solve() { cost.resize(n); for (auto &x : cost) { x.resize(n + 1); for (auto &y : x) y = 0; } match_memo.resize(n); for (auto &x : match_memo) { x.resize(n + 1); for (auto &y : x) y = -1; } /*memo_f.resize(kk + 1); for (auto &x : memo_f) { x.resize(n + 1); for (auto &y : x) y = -1; }*/ parse(0, n); for (int i = 0; i < n; i++) { for (auto &x : lists) { int count = 0; for (auto &y : x) { if (y.l < i) continue; if (y.broken) { count = 0; continue; } count++; cost[i][y.r] += count - 1; cost[i][y.r]++; } } for (int j = i + 1; j < n; j++) cost[i][j] += cost[i][j - 1]; } return dupa(kk, n - 1); // return f(kk, n - 1); } int solve2() { cin >> n >> kk; cin >> s; n = s.size(); return solve(); } #undef int int main() { ios::sync_with_stdio(false); cin.exceptions(cin.failbit); cin.tie(0); cout << solve2() << "\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 | #include <algorithm> #include <array> #include <cassert> #include <cstdio> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <ranges> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long LL; #define int LL typedef pair<int, int> PII; const int INF = 1e18; #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() int n, kk; string s; vector<vector<int>> cost; /* vector<vector<int>> memo_f; int f(int i, int j) { if (i < 0) return -INF; if (j < 0) return i == 0 ? 0 : -INF; int &res = memo_f[i][j]; if (res != -1) return res; res = INF; for (int k = 0; k <= j; k++) { int v = f(i - 1, k - 1); if (v == -INF) continue; res = min(res, cost[k][j] + f(i - 1, k - 1)); } return res; }*/ struct S { int l, r; bool broken; S(int _l, int _r, bool _broken = false) : l(_l), r(_r), broken(_broken){}; }; vector<vector<S>> lists; vector<vector<int>> match_memo; int match(int i, int sum) { if (i == s.size()) return INF; if (sum < 0) return INF; int &res = match_memo[i][sum]; if (res != -1) return res; sum += s[i] == '(' ? 1 : -1; if (sum == 0) return i; return res = match(i + 1, sum); } void parsed(vector<S> list) { if (!list.empty()) lists.push_back(list); } void parse(int l, int r) { assert(l < r); vector<S> list; while (true) { while (l < r && s[l] != '(') { list.push_back({S(l, l, true)}); l++; } if (l == r) break; assert(s[l] == '('); int j; for (j = l; j < r; j++) { if (s[j] == '(' && match(j, 0) < r) { while (l < j) { list.push_back({S(l, l, true)}); l++; } l = j; j = match(j, 0); break; } } if (j == r) break; assert(s[j] == ')'); list.push_back(S(l, j)); if (j != l + 1) parse(l + 1, j); l = j + 1; } parsed(list); } vector<vector<int>> dpf; void huj(int g, int i, int j, int l, int r) { if (i > j) return; int mid = (i + j) / 2; int best = l; for (int k = l; k <= min(r, mid); k++) { int val = dpf[g - 1][k - 1] + cost[k - 1][mid - 1]; if (val < dpf[g][mid]) { dpf[g][mid] = val; best = k; } } huj(g, i, mid - 1, l, best); huj(g, mid + 1, j, best, r); } int dupa(int k, int n) { n++; k++; dpf.resize(k + 1); for (auto &x : dpf) { x.resize(n + 1); for (auto &y : x) y = INF; } for (int i = 0; i <= k; i++) dpf[i][0] = i == 0; for (int j = 0; j <= n; j++) dpf[0][j] = INF; for (int i = 1; i <= k; i++) huj(i, 1, n, 1, n); return dpf[k][n]; } int solve() { cost.resize(n); for (auto &x : cost) { x.resize(n + 1); for (auto &y : x) y = 0; } match_memo.resize(n); for (auto &x : match_memo) { x.resize(n + 1); for (auto &y : x) y = -1; } /*memo_f.resize(kk + 1); for (auto &x : memo_f) { x.resize(n + 1); for (auto &y : x) y = -1; }*/ parse(0, n); for (int i = 0; i < n; i++) { for (auto &x : lists) { int count = 0; for (auto &y : x) { if (y.l < i) continue; if (y.broken) { count = 0; continue; } count++; cost[i][y.r] += count - 1; cost[i][y.r]++; } } for (int j = i + 1; j < n; j++) cost[i][j] += cost[i][j - 1]; } return dupa(kk, n - 1); // return f(kk, n - 1); } int solve2() { cin >> n >> kk; cin >> s; n = s.size(); return solve(); } #undef int int main() { ios::sync_with_stdio(false); cin.exceptions(cin.failbit); cin.tie(0); cout << solve2() << "\n"; } // ()( () | () ) () ((| )) // () (() ( | )) () (()) |