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

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        vector<pair<int,pair<int,int>>> P(0);
        int curr=0;
        bool ifbegin = true;
        int prev;
        char s;
        int t;
        bool ifpandemic=false;
        pair<int,pair<int,int>> temp;
        for(int i=0; i<n; i++)
        {
            cin >> s;
            t=(int)s-48;
            if(t==0){
                ++curr;
                if(i==n-1)
                {
                    temp.first=curr;
                    temp.second.first=-1;
                    temp.second.second=curr;
                    P.push_back(temp);
                }
                prev=t;
            }
            else
            {
                ifpandemic=true;
                if(i==0)
                {
                    ifbegin=false;
                    prev=t;
                    continue;
                }
                if(prev==1)
                    continue;
                if(ifbegin)
                {
                    ifbegin=false;
                    temp.first=curr;
                    temp.second.first=-1;
                    temp.second.second=curr;
                    P.push_back(temp);
                    curr=0;
                }else{
                    temp.first=(curr+1)/2;
                    temp.second.first=-2;
                    temp.second.second=curr;
                    P.push_back(temp);
                    curr=0;
                }
                prev = t;
            }
        }
        sort(P.begin(), P.end());
        int round=0;
        int saved=0;
        int type;
        int Howlong;
        for(int i=P.size()-1; i>=0; i--)
        {
            curr=P[i].second.second;
            type=P[i].second.first*-1;
            Howlong=P[i].first;
            if(type==1)
            {
                if(round>=Howlong)
                    break;
                saved+=(curr-round);
                ++round;
            }else{
                if(round>=Howlong)
                    break;
                saved+=(curr-2*round);
                if((curr-2*round)>1)
                    saved--;
                round+=2;

            }
        }
        //for(int i=P.size()-1; i>=0; i--)
            //cout << P[i].first << ' ' << P[i].second.first << ' ' << P[i].second.second << "\n";
        cout << (n-saved) << "\n";
    }
    return 0;
}