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
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* -----------------------
Autor: Tomasz Boguslawski
-------------------------- */
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<sstream>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include <fstream>
#include<math.h>

#define LL long long
#define FOR(x, b, e) for(LL x = b; x <= (e); x++)
#define FORD(x, b, e) for(LL x = b; x >= (e); x--)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define DEBUG if (debug)
#define MIN(a,b) ((a>b)?b:a)
#define MAX(a,b) ((a>b)?a:b)

using namespace std;

LL b1[1000001]; LL b1c;
LL b2[1000001]; LL b2c;
LL ile1start;

void detect(LL n, const string &s)
{
    ile1start=0; FOR(i,0,n-1) if (s[i]=='1') ile1start++;
    b1c = 0; b2c = 0;
    LL i = 0;
    while (i<=n-1)
    {
        if (s[i]=='1') break;
        i++;
    }
    if (i==s.length()) return;
    if (i>0) { b1c++; b1[b1c]=i; }
    LL state=1;
    LL beg=-1;
    FOR(j,i,n-1)
    {
        if (s[j]=='0')
        {
            if (state==1) { beg=j; state=0; }
        }
        else
        {
            if (state==0)
            {
                state=1;
                b2c++;
                b2[b2c]=j-beg;
                beg=-1;
            }
        }
    }
    if (state==0)
    {
        b1c++;
        b1[b1c]=n-beg;
    }
}

LL count_infected()
{
    LL active=b1c+b2c;
    LL infected=0;
    while (active>0)
    {
        //cout << " active=" << active << "\n";
        LL mx1=0; LL mx1n=0;
        FOR(i,1,b1c) if ((b1[i]>0)&&(b1[i]*2>mx1)) { mx1=b1[i]*2; mx1n=i; }
        LL mx2=0; LL mx2n=0;
        FOR(i,1,b2c) if ((b2[i]>0)&&((b2[i]+1)>mx2)) { mx2=(b2[i]+1); mx2n=i; }
        //cout << "mx1=" << mx1 << " o wartosci=" << b1[mx1n] << " mx2=" << mx2 << " o wartosci=" << b2[mx2n] << "\n";
        if (mx1>=mx2)
        {
            //cout << " szczepienie jedynki: " << b1[mx1n] << "\n";
            b1[mx1n]=0; active--;
        }
        else
        {
            //cout << " szczepienie dwojki: " << b2[mx2n] << "\n";
            LL mxs=b2[mx2n];
            b2[mx2n]=0;
            if (mxs==1) active--;
            else
            {
                // przekladamy do jedynek:
                b1c++;
                b1[b1c]=mxs-1;
            }
        }
        FOR(i,1,b1c)
        {
            if (b1[i]>0)
            {
                b1[i]--; infected++;
                if (b1[i]==0) active--;
            }
        }
        FOR(i,1,b2c)
        {
            if (b2[i]==1) { b2[i]=0; infected++; active--; }
            else if (b2[i]>1) { b2[i]-=2; infected+=2; if (b2[i]==0) active--; }
        }
    }
    return ile1start+infected;
}

/// MAIN
int main(int argc, char* argv[])
{
    // magic formula, which makes streams work faster:
	ios_base::sync_with_stdio(0);

	LL t; cin >> t;
	LL n; string s;
	FOR(pt, 1, t)
	{
        cin >> n;
        cin >> s;
        detect(n,s);
        cout << count_infected() << "\n";
	}

    return 0;
};