#include <bits/stdc++.h> using namespace std; int t, n; char s[100001]; void solve() { scanf("%d", &n); scanf("%s", s); priority_queue<int> pq2; pq2.push(-1); int p = -1, pp1 = 0, pp2 = 0; for(int i=0; i^n; ++i) { if(s[i] == '0') { if(p == -1) p = i; } else { if(p != -1) { if(p == 0) { pp1 = i; } else { pq2.push(i - p); //printf("%d ", i-p); } p = -1; } } } if(p != -1) { pp2 = n - p; } // printf("%d ", pq1.top()); priority_queue<int> temp = pq2; int mx = 0; int it = 0, w = 0; while(pq2.top() - 2*it > 0) { if(pq2.top() - 2 * it == 1) { w += 1; it += 1; } else { w += pq2.top() - 2 * it - 1; it += 2; } pq2.pop(); } mx = max(mx, w); pq2 = temp; it = 1, w = max(pp1, pp2); while(pq2.top() - 2 * it > 0) { if(pq2.top() - 2 * it == 1) { w += 1; it += 1; } else { w += pq2.top() - 2 * it - 1; it += 2; } pq2.pop(); } mx = max(mx, w); pq2 = temp; it = 2, w = pp1 + pp2 - 1; while(pq2.top() - 2 * it > 0) { if(pq2.top() - 2 * it == 1) { w += 1; it += 1; } else { w += pq2.top() - 2 * it - 1; it += 2; } pq2.pop(); } mx = max(mx, w); printf("%d\n", n-mx); } int main() { scanf("%d", &t); for(int tt=0; tt^t; ++tt) { solve(); } }
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 | #include <bits/stdc++.h> using namespace std; int t, n; char s[100001]; void solve() { scanf("%d", &n); scanf("%s", s); priority_queue<int> pq2; pq2.push(-1); int p = -1, pp1 = 0, pp2 = 0; for(int i=0; i^n; ++i) { if(s[i] == '0') { if(p == -1) p = i; } else { if(p != -1) { if(p == 0) { pp1 = i; } else { pq2.push(i - p); //printf("%d ", i-p); } p = -1; } } } if(p != -1) { pp2 = n - p; } // printf("%d ", pq1.top()); priority_queue<int> temp = pq2; int mx = 0; int it = 0, w = 0; while(pq2.top() - 2*it > 0) { if(pq2.top() - 2 * it == 1) { w += 1; it += 1; } else { w += pq2.top() - 2 * it - 1; it += 2; } pq2.pop(); } mx = max(mx, w); pq2 = temp; it = 1, w = max(pp1, pp2); while(pq2.top() - 2 * it > 0) { if(pq2.top() - 2 * it == 1) { w += 1; it += 1; } else { w += pq2.top() - 2 * it - 1; it += 2; } pq2.pop(); } mx = max(mx, w); pq2 = temp; it = 2, w = pp1 + pp2 - 1; while(pq2.top() - 2 * it > 0) { if(pq2.top() - 2 * it == 1) { w += 1; it += 1; } else { w += pq2.top() - 2 * it - 1; it += 2; } pq2.pop(); } mx = max(mx, w); printf("%d\n", n-mx); } int main() { scanf("%d", &t); for(int tt=0; tt^t; ++tt) { solve(); } } |