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
#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define vc vector
#define mt make_tuple
int main()
{
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        int n;
        cin>>n;
        string bin;
        cin>>bin;
        deque<int> z_zak,dif;
        for(int j=0;j<n;j++)
        {
            if(bin[j]=='1')
            {
                z_zak.pb(j);
            }
        }
        if(z_zak.size()>1)
        {
            for(int j=0;j<z_zak.size()-1;j++)
            {
                dif.pb(z_zak[j+1]-z_zak[j]);
            }
            int counter = 0 ;
            int SUM = 0;
            sort(dif.begin(),dif.end());
            std::reverse(dif.begin(),dif.end());
            if(z_zak[0]>1 && z_zak[z_zak.size()-1]<n-2)
            {
                counter+=4;
                SUM = z_zak[0] + (n-1-z_zak[z_zak.size()-1]) - 1;
            }
            else if(z_zak[0]>=1 && z_zak[z_zak.size()-1]>=n-2 )
            {
                counter+=2;
                SUM = z_zak[0];
            }
            else if(z_zak[0]<=1 && z_zak[z_zak.size()-1]<n-2)
            {
                counter+=2;
                SUM = n-1 - z_zak[z_zak.size()-1];
            }
            while(!dif.empty())
            {

            SUM += max(0,(dif[0]-counter-2));
            if(dif[0]-counter-2==0)
            {
                SUM+=1;
            }
            dif.pop_front();
            counter+=4;
            }
            cout<<n-SUM<<endl;
        }
        else if(z_zak.size()==1 && z_zak[0]!= 0 && z_zak[0]!=n-1)
        {
            cout<<2<<endl;;
        }
        else if(z_zak.size()==0)
        {
            cout<<0<<endl;
        }
        else
        {
            cout<<1<<endl;
        }

    }
}