#include<bits/stdc++.h> using namespace std; constexpr int MAX_N = 100000; char s[MAX_N+9]; vector<int> v; void solve() { int n; scanf("%d", &n); scanf("%s", s); v.clear(); int l = 0; int p = n-1; while(s[l] == '0' && l < n) { ++l; } if(l >= n) { printf("0\n"); return; } while(s[p] == '0') { --p; } int i = l; while(i < p) { if(s[i] == '1' ) { while(s[i] == '1') ++i; } else { int j = i; while(s[j] == '0') ++j; //printf("v.pb(%d)\n", j-i); v.push_back(j-i); i = j; } } p = n-p-1; //printf("l:%d\n", l); //printf("p:%d\n", p); sort(v.begin(), v.end()); if(l < p) { swap(l,p); } int wyn = 0; i = 0; while(v.size()) { if(2*l > v.back()) { wyn += max(0, l-i); ++i; l = 0; } if(2*p > v.back()) { wyn += max(0, p-i); ++i; p = 0; } if(v.back() >= 2*i+3) { wyn += v.back()-2*i-1; i += 2; } else if(v.back() > 2*i) { ++wyn; ++i; } else { break; } v.pop_back(); } if(l > p) { wyn += max(0, l-i); ++i; l = 0; wyn += max(0, p-i); ++i; p = 0; } else { wyn += max(0, p-i); ++i; p = 0; wyn += max(0, l-i); ++i; l = 0; } printf("%d\n", n-wyn); } int main() { int T; scanf("%d", &T); while(T--) { solve(); } } /* 1 1000 0000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000 //*/
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 | #include<bits/stdc++.h> using namespace std; constexpr int MAX_N = 100000; char s[MAX_N+9]; vector<int> v; void solve() { int n; scanf("%d", &n); scanf("%s", s); v.clear(); int l = 0; int p = n-1; while(s[l] == '0' && l < n) { ++l; } if(l >= n) { printf("0\n"); return; } while(s[p] == '0') { --p; } int i = l; while(i < p) { if(s[i] == '1' ) { while(s[i] == '1') ++i; } else { int j = i; while(s[j] == '0') ++j; //printf("v.pb(%d)\n", j-i); v.push_back(j-i); i = j; } } p = n-p-1; //printf("l:%d\n", l); //printf("p:%d\n", p); sort(v.begin(), v.end()); if(l < p) { swap(l,p); } int wyn = 0; i = 0; while(v.size()) { if(2*l > v.back()) { wyn += max(0, l-i); ++i; l = 0; } if(2*p > v.back()) { wyn += max(0, p-i); ++i; p = 0; } if(v.back() >= 2*i+3) { wyn += v.back()-2*i-1; i += 2; } else if(v.back() > 2*i) { ++wyn; ++i; } else { break; } v.pop_back(); } if(l > p) { wyn += max(0, l-i); ++i; l = 0; wyn += max(0, p-i); ++i; p = 0; } else { wyn += max(0, p-i); ++i; p = 0; wyn += max(0, l-i); ++i; l = 0; } printf("%d\n", n-wyn); } int main() { int T; scanf("%d", &T); while(T--) { solve(); } } /* 1 1000 0000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000 //*/ |