#include <iostream> #include <vector> using namespace std; int** vals; int getNum(int a) { int out = 1; out += a; out *= a; out /= 2; return out; } pair<int, int> getVal(int s, int n, string& naw, int ss, int d = 0, int cs = 0) { int count = 0; int poss = 0; vals[ss][s] = cs; pair<int, int> tmp; for (int i = s; i < n; i++) { if (i == 25) { poss = poss; } if (naw[i] == '(') { vals[ss][i] = poss + cs; tmp = getVal(i + 1, n, naw, ss, d + 1, poss + cs); count++; i = tmp.second; poss += tmp.first; poss -= getNum(count - 1); poss += getNum(count); vals[ss][i] = poss + cs; continue; } if (naw[i] == ')' && d > 0) { return { poss, i }; } if (naw[i] == ')' && d == 0) { count = 0; vals[ss][i] = poss + cs; } } return { poss, n }; } int main() { int n, k; cin >> n >> k; string naw; cin >> naw; vals = new int* [n+1]; for (int i = 0; i < n; i++) { vals[i] = new int[n+1] {0}; getVal(i, n, naw, i); } /*for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << vals[i][j] << " "; } cout << endl; }*/ int** dp = new int* [n]; for (int i = 0; i < n; i++) { dp[i] = new int[n]; for (int j = 0; j < n; j++) { dp[i][j] = 1e7; } } for (int i = 0; i < n; i++) { dp[i][0] = vals[0][i]; } int tmp, tmp2; for (int i = 1; i < n; i++) { for (int j = i; j < n; j++) { for (int l = 1; l < k && l <= i; l++) { tmp = dp[i - 1][l - 1]; tmp2 = vals[i][j]; if (tmp + tmp2 < dp[j][l]) { dp[j][l] = tmp + tmp2; } } } } cout << dp[n - 1][k - 1]; }
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 | #include <iostream> #include <vector> using namespace std; int** vals; int getNum(int a) { int out = 1; out += a; out *= a; out /= 2; return out; } pair<int, int> getVal(int s, int n, string& naw, int ss, int d = 0, int cs = 0) { int count = 0; int poss = 0; vals[ss][s] = cs; pair<int, int> tmp; for (int i = s; i < n; i++) { if (i == 25) { poss = poss; } if (naw[i] == '(') { vals[ss][i] = poss + cs; tmp = getVal(i + 1, n, naw, ss, d + 1, poss + cs); count++; i = tmp.second; poss += tmp.first; poss -= getNum(count - 1); poss += getNum(count); vals[ss][i] = poss + cs; continue; } if (naw[i] == ')' && d > 0) { return { poss, i }; } if (naw[i] == ')' && d == 0) { count = 0; vals[ss][i] = poss + cs; } } return { poss, n }; } int main() { int n, k; cin >> n >> k; string naw; cin >> naw; vals = new int* [n+1]; for (int i = 0; i < n; i++) { vals[i] = new int[n+1] {0}; getVal(i, n, naw, i); } /*for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << vals[i][j] << " "; } cout << endl; }*/ int** dp = new int* [n]; for (int i = 0; i < n; i++) { dp[i] = new int[n]; for (int j = 0; j < n; j++) { dp[i][j] = 1e7; } } for (int i = 0; i < n; i++) { dp[i][0] = vals[0][i]; } int tmp, tmp2; for (int i = 1; i < n; i++) { for (int j = i; j < n; j++) { for (int l = 1; l < k && l <= i; l++) { tmp = dp[i - 1][l - 1]; tmp2 = vals[i][j]; if (tmp + tmp2 < dp[j][l]) { dp[j][l] = tmp + tmp2; } } } } cout << dp[n - 1][k - 1]; } |