#include <iostream> #include <vector> #include <algorithm> #include <string> #include <map> #include <set> #include <chrono> #include <functional> #include <cstdlib> #include <cassert> #include <sstream> #include <climits> using namespace std; struct Elem { int height = 0; int count = 0; }; struct NawiasowoscInfo { long long minnawiasowosc = 0; int maxLeftPodzial = 0; }; int main() { //ios::sync_with_stdio(false); //std::cin.ignore(256, '\n'); const clock_t begin_time = clock(); int n; int k; std::cin >> n >> k; k--; std::string nawiasy; std::cin >> nawiasy; std::vector<int> peaks; std::vector<int> ext_peaks; ext_peaks.push_back(0); for (int i = 0; i < nawiasy.size() - 1; ++i) { if (nawiasy[i] == '(' && nawiasy[i+1] == ')') { peaks.push_back(i+1); ext_peaks.push_back(i+1); } } if (peaks.size() <= k) { std::cout << "0\n"; return 0; } std::vector<std::vector<long long>> nawiasowosc(nawiasy.size()); for (int i=0; i< ext_peaks.size(); ++i) { int start = ext_peaks[i]; nawiasowosc[start].resize(nawiasy.size()+1); std::vector<Elem> elements; int height = 0; long long count = 0; elements.push_back({height, 1 }); for (int j = start; j < nawiasy.size(); ++j) { if (nawiasy[j] == '(') { height++; elements.push_back({height, 1}); } else { height--; if (elements.size() > 0) { elements.pop_back(); } if (elements.size() > 0) { count += elements.back().count; ++elements.back().count; } else { elements.push_back({ height, 1 }); } } nawiasowosc[start][j + 1] = count; } } ext_peaks.push_back(nawiasy.size()); std::vector<std::vector<NawiasowoscInfo>> table(ext_peaks.size()); for (int ni = 0; ni < ext_peaks.size(); ++ni) { table[ni].resize(k+1); table[ni][0].minnawiasowosc = nawiasowosc[0][ext_peaks[ni]]; table[ni][0].maxLeftPodzial = 0; } for (int ki = 1; ki <= k; ++ki) { table[0][ki].minnawiasowosc = 0; table[0][ki].maxLeftPodzial = 0; for (int ni = 1; ni < ext_peaks.size(); ++ni) { int lastPodzial = table[ni-1][k].maxLeftPodzial; long long minNawiasowosc = LLONG_MAX; int bestPodzial; for (int p = lastPodzial; p < ni; ++p) { long long nawias = table[p][ki-1].minnawiasowosc + nawiasowosc[ext_peaks[p]][ext_peaks[ni]]; if (nawias <= minNawiasowosc) { minNawiasowosc = nawias; bestPodzial = p; } } table[ni][ki].minnawiasowosc = minNawiasowosc; table[ni][ki].maxLeftPodzial = bestPodzial; } } std::cout << table[ext_peaks.size()-1][k].minnawiasowosc << "\n"; //std::vector<std::vector<long long>> //testMaxTest(); //testBigNumbers(); //testFractions(); //std::cout << float(clock() - begin_time) / CLOCKS_PER_SEC << "\n"; 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 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 | #include <iostream> #include <vector> #include <algorithm> #include <string> #include <map> #include <set> #include <chrono> #include <functional> #include <cstdlib> #include <cassert> #include <sstream> #include <climits> using namespace std; struct Elem { int height = 0; int count = 0; }; struct NawiasowoscInfo { long long minnawiasowosc = 0; int maxLeftPodzial = 0; }; int main() { //ios::sync_with_stdio(false); //std::cin.ignore(256, '\n'); const clock_t begin_time = clock(); int n; int k; std::cin >> n >> k; k--; std::string nawiasy; std::cin >> nawiasy; std::vector<int> peaks; std::vector<int> ext_peaks; ext_peaks.push_back(0); for (int i = 0; i < nawiasy.size() - 1; ++i) { if (nawiasy[i] == '(' && nawiasy[i+1] == ')') { peaks.push_back(i+1); ext_peaks.push_back(i+1); } } if (peaks.size() <= k) { std::cout << "0\n"; return 0; } std::vector<std::vector<long long>> nawiasowosc(nawiasy.size()); for (int i=0; i< ext_peaks.size(); ++i) { int start = ext_peaks[i]; nawiasowosc[start].resize(nawiasy.size()+1); std::vector<Elem> elements; int height = 0; long long count = 0; elements.push_back({height, 1 }); for (int j = start; j < nawiasy.size(); ++j) { if (nawiasy[j] == '(') { height++; elements.push_back({height, 1}); } else { height--; if (elements.size() > 0) { elements.pop_back(); } if (elements.size() > 0) { count += elements.back().count; ++elements.back().count; } else { elements.push_back({ height, 1 }); } } nawiasowosc[start][j + 1] = count; } } ext_peaks.push_back(nawiasy.size()); std::vector<std::vector<NawiasowoscInfo>> table(ext_peaks.size()); for (int ni = 0; ni < ext_peaks.size(); ++ni) { table[ni].resize(k+1); table[ni][0].minnawiasowosc = nawiasowosc[0][ext_peaks[ni]]; table[ni][0].maxLeftPodzial = 0; } for (int ki = 1; ki <= k; ++ki) { table[0][ki].minnawiasowosc = 0; table[0][ki].maxLeftPodzial = 0; for (int ni = 1; ni < ext_peaks.size(); ++ni) { int lastPodzial = table[ni-1][k].maxLeftPodzial; long long minNawiasowosc = LLONG_MAX; int bestPodzial; for (int p = lastPodzial; p < ni; ++p) { long long nawias = table[p][ki-1].minnawiasowosc + nawiasowosc[ext_peaks[p]][ext_peaks[ni]]; if (nawias <= minNawiasowosc) { minNawiasowosc = nawias; bestPodzial = p; } } table[ni][ki].minnawiasowosc = minNawiasowosc; table[ni][ki].maxLeftPodzial = bestPodzial; } } std::cout << table[ext_peaks.size()-1][k].minnawiasowosc << "\n"; //std::vector<std::vector<long long>> //testMaxTest(); //testBigNumbers(); //testFractions(); //std::cout << float(clock() - begin_time) / CLOCKS_PER_SEC << "\n"; return 0; }; |