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
#include <algorithm>
#include <iostream>
#include <set>

using namespace std;

int TestCase() {
    int n; cin >> n;
    string s; cin >> s;
    multiset<int, greater<int>>twoSides, oneSide;
    int firstOneIndex = find(s.begin(), s.end(), '1') - s.begin();
    int lastOneIndex = s.rend() - find(s.rbegin(), s.rend(), '1') - 1;
    if(firstOneIndex == n)
        return 0;
    oneSide.insert(firstOneIndex);
    oneSide.insert(n - lastOneIndex - 1);
    
    for(int i = firstOneIndex; i < lastOneIndex; i++) {
        if(s[i] == '0') {
            int j = i;
            while(s[j] == '0')
                j++;
            twoSides.insert(j - i);
            i = j;
        }
    }
        
    
    
    
    int days = 0, result = 0;
    while(!oneSide.empty() || !twoSides.empty()) {
        while(!oneSide.empty() && *oneSide.begin() - days <= 0)
            oneSide.erase(oneSide.begin());
        while(!twoSides.empty() && *twoSides.begin() - 2 * days <= 0)
            twoSides.erase(twoSides.begin());
        
        if(!oneSide.empty() && !twoSides.empty()) {
            int one = *oneSide.begin() - days, two = *twoSides.begin() - 2 * days;
            if(two < one * 2) {
                result += one;
                oneSide.erase(oneSide.begin());
            } else {
                twoSides.erase(twoSides.begin());
                result++;
                oneSide.insert(two - 1 + days);
            }
        } else if(!oneSide.empty()) {
            int one = *oneSide.begin() - days;
            result += one;
            oneSide.erase(oneSide.begin());
        } else if(!twoSides.empty()) {
            int two = *twoSides.begin() - 2 * days;
            twoSides.erase(twoSides.begin());
            result++;
            oneSide.insert(two - 1 + days);
        }
        days++;
    }
    
    return n - result;
}

int main() {
    ios_base::sync_with_stdio(0);
    int t; cin >> t;
    while(t--)
        cout << TestCase() << '\n';
    
    return 0;
}