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
#include <cstdio>
#include <map>

using namespace std;

void run(int n) {
    multimap<long, long> zeroStrs;
    long start = -1;
    for (int j = 0; j < n; ++j) {
        char c = getchar();
        if (c == '0' && start == -1) start = j;
        else if (c != '0' && start > -1) {
            zeroStrs.emplace(j-start, start);
            start = -1;
        }
    }
    if (start > -1) {
        zeroStrs.emplace(n-start, start);
    }
    getchar();
    long steps = 0, saved = 0;
    for (auto it = zeroStrs.rbegin(); it != zeroStrs.rend(); ++it) {
        long len = it->first - steps;
        if(len <= 0) break;
        if (it->second == 0 || it->first + it->second >= n) {
            steps += 1;
            saved += len;
            continue;
        }
        steps += 2;
        saved += len - 1;
    }
    printf("%ld\n", n - saved);
}

int main() {
    int t;
    scanf("%d", &t);
    for(int i = 0; i < t; ++i) {
        int n;
        scanf("%d", &n); getchar();
        run(n);
    }
    return 0;
}