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
#include <bits/stdc++.h>
using namespace std;
int t, n, first, last, cur, res, days, mod;
string bajtocja;
vector<int> chore, pq;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> t;
    while (t--)
    {
        chore.clear();
        pq.clear();
        first = -10, last = -10, cur = 0, res = 0, days = 0, mod = 0;
        cin >> n >> bajtocja;
        for (int i = 0; i < n; ++i)
            if (bajtocja[i] == '1')
                chore.emplace_back(i);
        if (bajtocja[0] == '0' && chore.size())
            first = chore[0];
        if (bajtocja[n - 1] == '0' && chore.size())
            last = n - chore.back() - 1;
        if (chore.size() > 1)
            for (int i = 0; i < chore.size() - 1; ++i)
                pq.emplace_back(chore[i + 1] - chore[i] - 1);
        else
            pq.emplace_back(0);
        sort(pq.begin(), pq.end(), greater<int>());
        while (true)
        {
            if (!chore.size())
            {
                res = n;
                break;
            }
            pq[cur] += mod;
            if (first >= last)
            {
                if (first - days > 0 && first >= pq[cur] - 2 - days || cur >= pq.size())
                {
                    if (first - days <= 0)
                    {
                        if (mod)
                            ++res;
                        break;
                    }
                    res += first - days;
                    first = -10;
                }
                else
                {
                    if (pq[cur] - days * 2 <= 0)
                    {
                        if (mod)
                            ++res;
                        break;
                    }
                    if (mod)
                    {
                        res += pq[cur] - days * 2;
                        ++cur;
                    }
                    ++mod %= 2;
                }
            }
            else
            {
                if (last - days > 0 && last >= pq[cur] - 2 - days || cur >= pq.size())
                {
                    if (last - days <= 0)
                    {
                        if (mod)
                            ++res;
                        break;
                    }
                    res += last - days;
                    last = -10;
                }
                else
                {
                    if (pq[cur] - days * 2 <= 0)
                    {
                        if (mod)
                            ++res;
                        break;
                    }
                    if (mod)
                    {
                        res += pq[cur] - days * 2;
                        ++cur;
                    }
                    ++mod %= 2;
                }
            }
            ++days;
        }
        cout << n - res << '\n';
    }
}