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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <bits/stdc++.h>

using namespace std;

const int MAX = 1e5 + 1223;

char tab[MAX];

#define clog if (0) cerr

int maxa = 0;

int eval_kolejke(auto& kolejka, int tura, int co_wsadzic)
{
    kolejka.insert(co_wsadzic);
    int val = 0;
    int xd = 0;
    for (auto p : kolejka)
    {
        int ile = max(0, p - xd - tura);
        if (ile > 0)
        {
            ++xd;
            val += ile;
        }
        else
            break;
    }

    kolejka.erase(kolejka.find(co_wsadzic));

    return val;
}


void solve()
{
    int N; cin >> N;

    for (int i = 0; i < N; ++i)
        cin >> tab[i];

    multiset <int, greater<>> srodek;
    multiset <int, greater<>> brzeg;

    for (int i = 0; i < N; ++i)
    {
        if (tab[i] == '0')
        {
            int j = i;

            for (; j < N && tab[j] == '0'; ++j) {}

            auto ile = j - i;

            bool brzegb = (i == 0 || j == N);

            // cout << "ILE= " << ile << " " << j << "\n";

            if (brzegb)
                brzeg.insert(ile);
            else
                srodek.insert(ile);

            i = j - 1;
        }
    }

    int res = 0;
    int tura = 0;

    int curr_state = 0;
    int xd = 0;
    for (auto p : brzeg)
    {
        int ile = max(0, p - xd);
        ++xd;
        curr_state += ile;
    } 

    clog << "CURR_STATE= " << curr_state << endl;

    while(srodek.size())
    {
        int ile_min = (int)brzeg.size() + tura;

        while(brzeg.size() && (*--brzeg.end()) < ile_min)
        {
            brzeg.erase(--brzeg.end());
            ile_min = (int)brzeg.size() + tura;
        }

        auto p = (*srodek.begin()) - 2 * tura;
        int nowy_stan = eval_kolejke(brzeg, tura + 1, p - 1 + tura) + 1;

        clog << curr_state << " " << nowy_stan << endl;

        if (nowy_stan > curr_state)
        {
            clog << "Wszadzam= " << p - 1 + tura << "\n";
            curr_state = nowy_stan;
            brzeg.insert(p - 1 + tura);
            srodek.erase(srodek.begin());
            ++tura;
            res += 1;
        }
        else
        {
            break;
        }
    }

    for (auto p : brzeg)
    {
        clog << "BRZEG: " << p << " TURA= " << tura << "\n";
        int ilezostalo = max(0, p - tura);

        if (ilezostalo)
        {
            res += ilezostalo;
            ++tura;
        }

        clog << "Wynik: " << res << "\n\n\n";
    }

    for (auto p : srodek)
    {
        int zostalo = p - 2 * tura;
        if (zostalo > 0)
        {
            res++;
            tura++;
            zostalo-=2;

            if (zostalo > 0)
            {
                tura++;
                res += zostalo;
            }
        }
    }



    cout << N - res << "\n";
}


int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int T;
    cin >> T;

    for (int i = 0; i < T; ++i)
    {
        solve();
    }
}