#include <bits/stdc++.h> using namespace std; int INF = numeric_limits<int>::max(); int length, groups; string word; vector<int> maxs; vector<vector<int>> ranges; vector<vector<pair<int, int>>> dp; void read() { cin >> length >> groups; cin >> word; } void findMaxs() { for (int i = 0; i < length - 1; ++i) { if (word[i] == '(' && word[i + 1] == ')') { maxs.push_back(i + 1); } } } void computeRangesForStart(int index) { stack<int> stack; stack.push(1); vector<int> row; int result = 0; int maxIndex = index; int start = (index == 0) ? 0 : maxs[index-1]; for (int i = start; i < length; ++i) { if (word[i] == '(') { stack.push(1); } else { if (!stack.empty()) { stack.pop(); } if (!stack.empty()) { result += stack.top(); stack.top()++; } else { stack.push(1); } } if (i == maxs[maxIndex] - 1 || i == length - 1) { row.push_back(result); maxIndex++; } } ranges.push_back(row); } void computeRanges() { for (int i = 0; i <= maxs.size(); ++i) { computeRangesForStart(i); } } void dynamic() { for (int i = 0; i < groups; ++i) { vector<pair<int, int>> row; dp.push_back(row); } for (int i = 0; i <= maxs.size(); ++i) { for (int j = 0; j < groups; ++j) { if (j > i) { dp[j].emplace_back(INF, 0); } else if (i == 0) { dp[j].emplace_back(0, 0); } else if (j == 0) { dp[j].emplace_back(ranges[0][i], 0); } else { int sum = INF, index = dp[j][i - 1].second; for (int k = index; k < i; ++k) { if (dp[j - 1][k].first < INF) { int current = dp[j - 1][k].first + ranges[k + 1][i - k - 1]; if (current <= sum) { sum = current; index = k; } } } dp[j].emplace_back(sum, index); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); read(); findMaxs(); if (maxs.size() < groups) { cout << 0 << endl; return 0; } computeRanges(); dynamic(); cout << dp[groups - 1][maxs.size()].first << 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 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 | #include <bits/stdc++.h> using namespace std; int INF = numeric_limits<int>::max(); int length, groups; string word; vector<int> maxs; vector<vector<int>> ranges; vector<vector<pair<int, int>>> dp; void read() { cin >> length >> groups; cin >> word; } void findMaxs() { for (int i = 0; i < length - 1; ++i) { if (word[i] == '(' && word[i + 1] == ')') { maxs.push_back(i + 1); } } } void computeRangesForStart(int index) { stack<int> stack; stack.push(1); vector<int> row; int result = 0; int maxIndex = index; int start = (index == 0) ? 0 : maxs[index-1]; for (int i = start; i < length; ++i) { if (word[i] == '(') { stack.push(1); } else { if (!stack.empty()) { stack.pop(); } if (!stack.empty()) { result += stack.top(); stack.top()++; } else { stack.push(1); } } if (i == maxs[maxIndex] - 1 || i == length - 1) { row.push_back(result); maxIndex++; } } ranges.push_back(row); } void computeRanges() { for (int i = 0; i <= maxs.size(); ++i) { computeRangesForStart(i); } } void dynamic() { for (int i = 0; i < groups; ++i) { vector<pair<int, int>> row; dp.push_back(row); } for (int i = 0; i <= maxs.size(); ++i) { for (int j = 0; j < groups; ++j) { if (j > i) { dp[j].emplace_back(INF, 0); } else if (i == 0) { dp[j].emplace_back(0, 0); } else if (j == 0) { dp[j].emplace_back(ranges[0][i], 0); } else { int sum = INF, index = dp[j][i - 1].second; for (int k = index; k < i; ++k) { if (dp[j - 1][k].first < INF) { int current = dp[j - 1][k].first + ranges[k + 1][i - k - 1]; if (current <= sum) { sum = current; index = k; } } } dp[j].emplace_back(sum, index); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); read(); findMaxs(); if (maxs.size() < groups) { cout << 0 << endl; return 0; } computeRanges(); dynamic(); cout << dp[groups - 1][maxs.size()].first << endl; return 0; } |