#include <iostream> #include <string> #include <algorithm> #include <vector> using namespace std; int n, l, r; vector<int> mid; int solve_r(int& time) { int result = max(r-time, 0); ++time; return result; } int solve_l(int& time) { int result = max(l-time, 0); ++time; return result; } int solve_mid(int& time) { int result = 0; for(auto el: mid) { if(el > 2*time) { if(el > 2*(time+1)) { result += el-2*time - 1; time += 2; } else { ++result; ++time; } } } return result; } int l_mid_r() { int time = 0; int result = solve_l(time); result += solve_mid(time); result += solve_r(time); return result; } int l_r_mid() { int time = 0; int result = 0; result += solve_l(time); result += solve_r(time); result += solve_mid(time); return result; } int mid_l_r() { int time = 0; int result = 0; result += solve_mid(time); result += solve_l(time); result += solve_r(time); return result; } int mid_r_l() { int time = 0; int result = 0; result += solve_mid(time); result += solve_r(time); result += solve_l(time); return result; } int r_mid_l() { int time = 0; int result = 0; result += solve_r(time); result += solve_mid(time); result += solve_l(time); return result; } int r_l_mid() { int time = 0; int result = 0; result += solve_r(time); result += solve_l(time); result += solve_mid(time); return result; } int main() { ios_base::sync_with_stdio(false); int t; cin >> t; while(t--) { mid.clear(); cin >> n; string input; cin >> input; int ind = 0, prev; while(ind < input.size() && input[ind] != '1') { ++ind; } l = ind; prev = ind++; for(; ind < input.size(); ++ind) { if(input[ind] == '1') { if(ind-prev-1 > 0) { mid.push_back(ind-prev-1); } prev = ind; } } r = ind - prev-1; // cout << l << " " << mid.size() << " " << r << endl; int time = 0; sort(mid.begin(), mid.end()); reverse(mid.begin(), mid.end()); // cout << input << ": " << solve_mid(time) << endl; // cout << time << endl; int res = 0; res = max(res, l_mid_r()); res = max(res, l_r_mid()); res = max(res, r_l_mid()); res = max(res, r_mid_l()); res = max(res, mid_l_r()); res = max(res, mid_r_l()); // cout << input << ": " << res << endl; cout << n-res << endl; } 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 135 136 137 138 | #include <iostream> #include <string> #include <algorithm> #include <vector> using namespace std; int n, l, r; vector<int> mid; int solve_r(int& time) { int result = max(r-time, 0); ++time; return result; } int solve_l(int& time) { int result = max(l-time, 0); ++time; return result; } int solve_mid(int& time) { int result = 0; for(auto el: mid) { if(el > 2*time) { if(el > 2*(time+1)) { result += el-2*time - 1; time += 2; } else { ++result; ++time; } } } return result; } int l_mid_r() { int time = 0; int result = solve_l(time); result += solve_mid(time); result += solve_r(time); return result; } int l_r_mid() { int time = 0; int result = 0; result += solve_l(time); result += solve_r(time); result += solve_mid(time); return result; } int mid_l_r() { int time = 0; int result = 0; result += solve_mid(time); result += solve_l(time); result += solve_r(time); return result; } int mid_r_l() { int time = 0; int result = 0; result += solve_mid(time); result += solve_r(time); result += solve_l(time); return result; } int r_mid_l() { int time = 0; int result = 0; result += solve_r(time); result += solve_mid(time); result += solve_l(time); return result; } int r_l_mid() { int time = 0; int result = 0; result += solve_r(time); result += solve_l(time); result += solve_mid(time); return result; } int main() { ios_base::sync_with_stdio(false); int t; cin >> t; while(t--) { mid.clear(); cin >> n; string input; cin >> input; int ind = 0, prev; while(ind < input.size() && input[ind] != '1') { ++ind; } l = ind; prev = ind++; for(; ind < input.size(); ++ind) { if(input[ind] == '1') { if(ind-prev-1 > 0) { mid.push_back(ind-prev-1); } prev = ind; } } r = ind - prev-1; // cout << l << " " << mid.size() << " " << r << endl; int time = 0; sort(mid.begin(), mid.end()); reverse(mid.begin(), mid.end()); // cout << input << ": " << solve_mid(time) << endl; // cout << time << endl; int res = 0; res = max(res, l_mid_r()); res = max(res, l_r_mid()); res = max(res, r_l_mid()); res = max(res, r_mid_l()); res = max(res, mid_l_r()); res = max(res, mid_r_l()); // cout << input << ": " << res << endl; cout << n-res << endl; } return 0; } |