#include <iostream> #include <vector> #include <algorithm> using namespace std; typedef long long int lli; int minOnes; int ones(string s) { int res = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '1') { res++; } } return res; } void despread(string &s, vector<int> indices) { for (int index : indices) { s[index] = '0'; } } vector<int> spread(string &s) { vector<int> indices; char prev = 'x'; for (int i = 0; i < s.size(); i++) { char next = i < s.size()-1 ? s[i+1] : 'x'; char curr = s[i]; if (curr == '0' && (prev == '1' || next == '1')) { s[i] = '1'; indices.push_back(i); } prev = curr; } return indices; } bool isOver(string s) { char prev = 'x'; for (int i = 0; i < s.size(); i++) { char curr = s[i]; char next = i < s.size() - 1 ? s[i+1] : 'x'; if (curr == '0' && (prev == '1' || next == '1')) { return false; } prev = curr; } return true; } void minimum(string s, int xs, int ones) { if (isOver(s)) { minOnes = min(minOnes, ones); return; } char prev = 'x'; for (int i = 0; i < s.size(); i++) { char curr = s[i]; char next = i < s.size()-1 ? s[i+1] : 'x'; if (curr == '0' && (prev == '1' || next == '1')) { s[i] = 'x'; vector<int> indices = spread(s); minimum(s, xs + 1, ones + indices.size()); despread(s, indices); s[i] = '0'; } prev = curr; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); lli tests; cin >> tests; for (int t = 0; t < tests; t++) { lli n; cin >> n; string s; cin >> s; minOnes = INT32_MAX; int onesCount = ones(s); if (onesCount == 0) { cout << 0 << '\n'; continue; } minimum(s, 0, onesCount); cout << minOnes << '\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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; typedef long long int lli; int minOnes; int ones(string s) { int res = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '1') { res++; } } return res; } void despread(string &s, vector<int> indices) { for (int index : indices) { s[index] = '0'; } } vector<int> spread(string &s) { vector<int> indices; char prev = 'x'; for (int i = 0; i < s.size(); i++) { char next = i < s.size()-1 ? s[i+1] : 'x'; char curr = s[i]; if (curr == '0' && (prev == '1' || next == '1')) { s[i] = '1'; indices.push_back(i); } prev = curr; } return indices; } bool isOver(string s) { char prev = 'x'; for (int i = 0; i < s.size(); i++) { char curr = s[i]; char next = i < s.size() - 1 ? s[i+1] : 'x'; if (curr == '0' && (prev == '1' || next == '1')) { return false; } prev = curr; } return true; } void minimum(string s, int xs, int ones) { if (isOver(s)) { minOnes = min(minOnes, ones); return; } char prev = 'x'; for (int i = 0; i < s.size(); i++) { char curr = s[i]; char next = i < s.size()-1 ? s[i+1] : 'x'; if (curr == '0' && (prev == '1' || next == '1')) { s[i] = 'x'; vector<int> indices = spread(s); minimum(s, xs + 1, ones + indices.size()); despread(s, indices); s[i] = '0'; } prev = curr; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); lli tests; cin >> tests; for (int t = 0; t < tests; t++) { lli n; cin >> n; string s; cin >> s; minOnes = INT32_MAX; int onesCount = ones(s); if (onesCount == 0) { cout << 0 << '\n'; continue; } minimum(s, 0, onesCount); cout << minOnes << '\n'; } return 0; } |