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
#include <bits/stdc++.h>

using namespace std;

int numberOfCities;
int leftOne;
int rightOne;
vector<int> toSave;

void Input() {
    cin >> numberOfCities;
    string s;
    cin >> s;
    leftOne = 0;
    rightOne = 0;
    toSave.clear();
    int counter = 0;
    for (int i = 0; i < numberOfCities; i++) {
        if (s[i] == '1') {
            if (counter) {
                if (s[0] == '0' && leftOne == 0) {
                    leftOne = counter;
                } else {
                    toSave.push_back(counter);
                }
                counter = 0;
            }
        } else {
            counter++;
        }
    }
    if (s[0] == '0' && leftOne == 0) {
        leftOne = counter;
        return;
    }
    if (s[numberOfCities - 1] == '0') {
        rightOne = counter;
    }
}

int Result(int round) {
    int answer = 0;
    for (int i = 0; i < toSave.size(); i++) {
        int toAdd = toSave[i] - 2 * round;
        if (toAdd <= 0) {
            break;
        }
        if (toAdd == 1 || toAdd == 2) {
            round++;
            answer++;
            continue;
        }
        round += 2;
        answer += (toAdd - 1);
    }
    return answer;
}

void Solve() {
    sort(toSave.begin(), toSave.end(), greater<int>());
    int answer = 0;
    if (leftOne != 0) {
        if (rightOne != 0) {
            answer = max(max(leftOne + rightOne - 1 + Result(2), max(leftOne, rightOne) + Result(1)), Result(0));
        } else {
            answer = max(leftOne + Result(1), Result(0));
        }
    } else {
        if (rightOne != 0) {
            answer = max(rightOne + Result(1), Result(0));
        } else {
            answer = Result(0);
        }
    }
    cout << numberOfCities - answer << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    int tests;
    cin >> tests;
    while (tests--) {
        Input();
        Solve();
    }
    return 0;
}