#include <bits/stdc++.h> using namespace std; #define ff first #define ss second int n, k; vector <int> v; int last = -1; string s; int res = INT_MAX; void count() { v.emplace_back(n); int l = 0; int wyn = 0; for (int i = 0; i < k; i++) { for (int j = l; j <= v[i]; j++) { int cur = 0; for (int a = j; a <= min(n-1, v[i]); a++) { if (s[a] == '(') cur++; else cur--; if (cur == 0) { wyn++; } if (cur < 0) break; } } l = v[i]+1; } res = min(res, wyn); // cout << wyn << "\n"; // for (auto i : v) // cout << i << " "; // cout << "\n"; v.pop_back(); } void bt() { if ((int)v.size() == k-1) { count(); return; } for (int i = last+1; i < n-1; i++) { int slast = last; last = i; v.emplace_back(i); bt(); last = slast; v.pop_back(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; cin >> s; bt(); cout << res << "\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 | #include <bits/stdc++.h> using namespace std; #define ff first #define ss second int n, k; vector <int> v; int last = -1; string s; int res = INT_MAX; void count() { v.emplace_back(n); int l = 0; int wyn = 0; for (int i = 0; i < k; i++) { for (int j = l; j <= v[i]; j++) { int cur = 0; for (int a = j; a <= min(n-1, v[i]); a++) { if (s[a] == '(') cur++; else cur--; if (cur == 0) { wyn++; } if (cur < 0) break; } } l = v[i]+1; } res = min(res, wyn); // cout << wyn << "\n"; // for (auto i : v) // cout << i << " "; // cout << "\n"; v.pop_back(); } void bt() { if ((int)v.size() == k-1) { count(); return; } for (int i = last+1; i < n-1; i++) { int slast = last; last = i; v.emplace_back(i); bt(); last = slast; v.pop_back(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; cin >> s; bt(); cout << res << "\n"; } |