// 2022-5-naw-nawiasowe-przedzialy.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include <iostream> #include <utility> #include <map> #include <queue> #include <set> #include <stack> #include <unordered_set> #include <unordered_map> constexpr long long MAXN = 1e5 + 7; int hmend[MAXN]; int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0); std::string s; int n, k; std::cin >> n >> k >> s; std::vector<std::vector<long long> > dp; for (int i = 0; i <= n; i++) { dp.push_back(std::vector<long long>(k + 1, MAXN * MAXN)); } dp[0][0] = 0; std::stack<int> stck; long long dst; int bgn = 0; for (int i = 0; i < n; i++) { if (i > 0) { bgn = 1; } for (int j = bgn; j < k; j++) { //std::cout << "+" << i << ' ' << j << ' ' << dp[i][j] << '\n'; dst = dp[i][j]; if (dst == MAXN * MAXN) { break; } while (!stck.empty()) { stck.pop(); } if (i > 0) { hmend[i - 1] = 0; } for (int l = i; l < n; l++) { if (s[l] == ')') { if (stck.empty()) { hmend[l] = 0; } else { if (stck.top() == 0) { hmend[l] = 1; } else { hmend[l] = hmend[stck.top() - 1] + 1; } stck.pop(); dst += hmend[l]; } } else { stck.push(l); hmend[l] = 0; } if (j < k) { //x = ((hm + 1) | ((i + 1) << 32)); /*auto it = currmn.find(x); if (it == currmn.end() || it->second > dst) { pq.push({ -dst, x }); currmn[x] = dst; if (i == s.size() - 1) { res = dst; } }*/ //std::cout << '-' << l + 1 << ' ' << j + 1 << ' ' << dst << '\n'; dp[l + 1][j + 1] = std::min(dp[l + 1][j + 1], dst); } } } } long long res = dp[n][0]; for (auto u : dp[n]) { res = std::min(u, res); } std::cout << res << '\n'; } /* std::priority_queue<std::pair<long long, long long> > pq; std::unordered_set<long long> visited; std::unordered_map<long long, long long> currmn; std::string s; long long hmend[MAXN]; int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0); int n, k; std::cin >> n >> k >> s; pq.push({ 0ll, 0ll}); long long dst; long long id; long long hm; long long res = ((long long)MAXN )* ((long long)MAXN); std::stack<long long> stck; long long x; int hmit = 0, xd2=0; while (!pq.empty()) { hmit++; auto v = pq.top(); pq.pop(); if (visited.count(v.second) > 0) { continue; } xd2++; visited.insert(v.second); dst = -v.first; if (dst >= res) { std::cout << "!" << hmit << ' ' << xd2 << '\n'; std::cout << res << '\n'; return 0; } hm = v.second & ((1ll << 32) - 1); id = v.second >> 32; //std::cout << dst << ' ' << id << ' ' << hm << '\n'; if (id > 0) { hmend[id - 1] = 0; } for (long long i = id; i < s.size(); i++) { if (s[i] == ')') { if (stck.empty()) { hmend[i] = 0; } else { if (stck.top() == 0) { hmend[i] = 1; } else { hmend[i] = hmend[stck.top() - 1] + 1; } stck.pop(); dst += hmend[i]; } } else { stck.push(i); hmend[i] = 0; } if (hm < k) { x = ((hm + 1) | ((i + 1) << 32)); auto it = currmn.find(x); if (it == currmn.end() || it->second > dst) { pq.push({ -dst, x }); currmn[x] = dst; if (i == s.size() - 1) { res = dst; } } } } while (!stck.empty()) { stck.pop(); } } } */ // Run program: Ctrl + F5 or Debug > Start Without Debugging menu // Debug program: F5 or Debug > Start Debugging menu // Tips for Getting Started: // 1. Use the Solution Explorer window to add/manage files // 2. Use the Team Explorer window to connect to source control // 3. Use the Output window to see build output and other messages // 4. Use the Error List window to view errors // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
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 | // 2022-5-naw-nawiasowe-przedzialy.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include <iostream> #include <utility> #include <map> #include <queue> #include <set> #include <stack> #include <unordered_set> #include <unordered_map> constexpr long long MAXN = 1e5 + 7; int hmend[MAXN]; int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0); std::string s; int n, k; std::cin >> n >> k >> s; std::vector<std::vector<long long> > dp; for (int i = 0; i <= n; i++) { dp.push_back(std::vector<long long>(k + 1, MAXN * MAXN)); } dp[0][0] = 0; std::stack<int> stck; long long dst; int bgn = 0; for (int i = 0; i < n; i++) { if (i > 0) { bgn = 1; } for (int j = bgn; j < k; j++) { //std::cout << "+" << i << ' ' << j << ' ' << dp[i][j] << '\n'; dst = dp[i][j]; if (dst == MAXN * MAXN) { break; } while (!stck.empty()) { stck.pop(); } if (i > 0) { hmend[i - 1] = 0; } for (int l = i; l < n; l++) { if (s[l] == ')') { if (stck.empty()) { hmend[l] = 0; } else { if (stck.top() == 0) { hmend[l] = 1; } else { hmend[l] = hmend[stck.top() - 1] + 1; } stck.pop(); dst += hmend[l]; } } else { stck.push(l); hmend[l] = 0; } if (j < k) { //x = ((hm + 1) | ((i + 1) << 32)); /*auto it = currmn.find(x); if (it == currmn.end() || it->second > dst) { pq.push({ -dst, x }); currmn[x] = dst; if (i == s.size() - 1) { res = dst; } }*/ //std::cout << '-' << l + 1 << ' ' << j + 1 << ' ' << dst << '\n'; dp[l + 1][j + 1] = std::min(dp[l + 1][j + 1], dst); } } } } long long res = dp[n][0]; for (auto u : dp[n]) { res = std::min(u, res); } std::cout << res << '\n'; } /* std::priority_queue<std::pair<long long, long long> > pq; std::unordered_set<long long> visited; std::unordered_map<long long, long long> currmn; std::string s; long long hmend[MAXN]; int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0); int n, k; std::cin >> n >> k >> s; pq.push({ 0ll, 0ll}); long long dst; long long id; long long hm; long long res = ((long long)MAXN )* ((long long)MAXN); std::stack<long long> stck; long long x; int hmit = 0, xd2=0; while (!pq.empty()) { hmit++; auto v = pq.top(); pq.pop(); if (visited.count(v.second) > 0) { continue; } xd2++; visited.insert(v.second); dst = -v.first; if (dst >= res) { std::cout << "!" << hmit << ' ' << xd2 << '\n'; std::cout << res << '\n'; return 0; } hm = v.second & ((1ll << 32) - 1); id = v.second >> 32; //std::cout << dst << ' ' << id << ' ' << hm << '\n'; if (id > 0) { hmend[id - 1] = 0; } for (long long i = id; i < s.size(); i++) { if (s[i] == ')') { if (stck.empty()) { hmend[i] = 0; } else { if (stck.top() == 0) { hmend[i] = 1; } else { hmend[i] = hmend[stck.top() - 1] + 1; } stck.pop(); dst += hmend[i]; } } else { stck.push(i); hmend[i] = 0; } if (hm < k) { x = ((hm + 1) | ((i + 1) << 32)); auto it = currmn.find(x); if (it == currmn.end() || it->second > dst) { pq.push({ -dst, x }); currmn[x] = dst; if (i == s.size() - 1) { res = dst; } } } } while (!stck.empty()) { stck.pop(); } } } */ // Run program: Ctrl + F5 or Debug > Start Without Debugging menu // Debug program: F5 or Debug > Start Debugging menu // Tips for Getting Started: // 1. Use the Solution Explorer window to add/manage files // 2. Use the Team Explorer window to connect to source control // 3. Use the Output window to see build output and other messages // 4. Use the Error List window to view errors // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file |