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

using namespace std;

int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);

    int t; cin >> t;
    while(t--) {

        int n; cin >> n;
        string s; cin >> s;

        vector<pair<int, int>> seg;
        int j = -1;
        for(int i = 0; i < n; i++) {
            if(s[i] == '1') {
                if(j != -1) {
                    seg.push_back({j, i - 1});
                    j = -1;
                }
            }
            else 
            if(j == -1) {
                j = i;
            }
        }

        if(j != -1) seg.push_back({j, n - 1}), j = -1;

/*
        for(auto &[l, r] : seg) {
            cerr << l << ' ' << r << '\n';
        }
*/

        sort(seg.begin(), seg.end(), [&](const pair<int, int> &a, const pair<int, int> &b) {

            int la = a.second - a.first + 1;
            int lb = b.second - b.first + 1;

            if(a.first == 0 || a.second == n - 1)
                la <<= 1;

            if(b.first == 0 || b.second == n - 1)
                lb <<= 1;

            return la > lb;
        });

        int cnt = 0, ans = 0;

        for(int i = 0; i < (int) seg.size(); i++) {

            int c = seg[i].second - seg[i].first + 1, sus = 1;

            if(seg[i].first != 0)       c -= cnt;
            else                        sus = 0;

            if(seg[i].second != n - 1)  c -= cnt;
            else                        sus = 0;
        
            if(c == 1)  sus = 0;

            if(c > 0) {
//                cerr << i << ": " << seg[i].first << ' ' << seg[i].second << ' ' << c << ' ' << sus << '\n';
            
                ans += c - sus;
                cnt += 1 + sus;
            }

        }

//        cerr << "ans: " << ans << '\n';

        cout << n - ans << '\n';
            
    }

}