#include <iostream> #include <vector> #include <algorithm> using namespace std; long long T[3030][2]; int M=1000000007; char s[1000010]; vector<int> v; vector<int> vj; int main() { int n,t; cin >> t; while(t--) { cin >> n; cin >> s; v.clear(); vj.clear(); int a=-1,b=-1; for (int i=0;i<n;i++) { if (s[i]=='1') { a = i; break; } } for (int i=n-1;i>=0;i--) { if (s[i]=='1') { b = i; break; } } if (a == -1) { cout << 0 << endl; continue; } if (a > 0) vj.push_back(a); if (b < n - 1) vj.push_back(n - 1 - b); int c = 0; for (int i = a + 1; i <= b; i++) { if (s[i] == '0') c++; else { if (c > 0) v.push_back(c); c = 0; } } sort(vj.begin(), vj.end()); reverse(vj.begin(), vj.end()); sort(v.begin(), v.end()); reverse(v.begin(), v.end()); int d = 0; int res = 0; int i = 0; while (i < v.size()) { int left = v[i] - 2 * d; if (left <= 4) break; res += left - 1; i++; d += 2; } if (vj.size()<2)vj.push_back(0); if (vj.size()<2)vj.push_back(0); if (i < v.size()) { int left = v[i] - 2 * d; int v1 = (left == 3 || left == 4 ? max(0, (left - (left > 1 ? 1 : 0))) + max(0, vj[0] - (d + 2)) + max(0, vj[1] - (d + 3)) : max(0, (left - (left > 1 ? 1 : 0))) + max(0, vj[0] - (d + 1)) + max(0, vj[1] - (d + 2)) ); int v2 = max(0, vj[0] - (d)) + (left > 2 ? 1 : 0) + max(0, vj[1] - (d + 2)); int v3 = max(0, vj[0] - (d)) + max(0, vj[1] - (d + 1)); res += max(v1, max(v2, v3)); } else { res += max(0, vj[0] - (d)) + max(0, vj[1] - (d + 1)); } cout << n - res << endl; } }
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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; long long T[3030][2]; int M=1000000007; char s[1000010]; vector<int> v; vector<int> vj; int main() { int n,t; cin >> t; while(t--) { cin >> n; cin >> s; v.clear(); vj.clear(); int a=-1,b=-1; for (int i=0;i<n;i++) { if (s[i]=='1') { a = i; break; } } for (int i=n-1;i>=0;i--) { if (s[i]=='1') { b = i; break; } } if (a == -1) { cout << 0 << endl; continue; } if (a > 0) vj.push_back(a); if (b < n - 1) vj.push_back(n - 1 - b); int c = 0; for (int i = a + 1; i <= b; i++) { if (s[i] == '0') c++; else { if (c > 0) v.push_back(c); c = 0; } } sort(vj.begin(), vj.end()); reverse(vj.begin(), vj.end()); sort(v.begin(), v.end()); reverse(v.begin(), v.end()); int d = 0; int res = 0; int i = 0; while (i < v.size()) { int left = v[i] - 2 * d; if (left <= 4) break; res += left - 1; i++; d += 2; } if (vj.size()<2)vj.push_back(0); if (vj.size()<2)vj.push_back(0); if (i < v.size()) { int left = v[i] - 2 * d; int v1 = (left == 3 || left == 4 ? max(0, (left - (left > 1 ? 1 : 0))) + max(0, vj[0] - (d + 2)) + max(0, vj[1] - (d + 3)) : max(0, (left - (left > 1 ? 1 : 0))) + max(0, vj[0] - (d + 1)) + max(0, vj[1] - (d + 2)) ); int v2 = max(0, vj[0] - (d)) + (left > 2 ? 1 : 0) + max(0, vj[1] - (d + 2)); int v3 = max(0, vj[0] - (d)) + max(0, vj[1] - (d + 1)); res += max(v1, max(v2, v3)); } else { res += max(0, vj[0] - (d)) + max(0, vj[1] - (d + 1)); } cout << n - res << endl; } } |