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

int t, n;
string s;

int a1, a2, curtime;
vector<int> segments;

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

    cin >> t;
    for (int q = 0; q < t; ++q)
    {
        cin >> n >> s;

        curtime = 0;
        segments.clear();

        bool first = true;
        int counter = 0;
        for (int i = 0; i < n; ++i)
        {
            if (s[i] == '1')
            {
                if (first)
                {
                    a1 = counter;
                    first = false;
                }
                else
                    segments.push_back(counter);
                counter = 0;
            }
            else
                ++counter;
        }
        a2 = counter;

        if (first)
        {
            cout << "0\n";
            continue;
        }

        sort(segments.begin(), segments.end(), greater<int>());

        int result = segments.size() + 1;

        bool sidesSolved = false;

        for (int i = 0; i < segments.size(); ++i)
        {
            int actualSize = segments[i] - curtime * 2;

            if (actualSize >= 4)
            {
                ++result;
                curtime += 2;
            }

            else if (actualSize <= 3)
            {
                if (!sidesSolved)
                {
                    if (a1 > curtime)
                    {
                        result += curtime;
                        curtime++;
                    }
                    else
                        result += a1;

                    if (a2 > curtime)
                    {
                        result += curtime;
                        curtime++;
                    }
                    else
                        result += a2;

                    actualSize = segments[i] - curtime * 2;
                    sidesSolved = true;
                }

                if (actualSize >= 1)
                {
                    ++result;
                    ++curtime;
                }

                else
                    result += segments[i];
            }
        }

        if (!sidesSolved)
        {
            if (a1 > curtime)
            {
                result += curtime;
                curtime++;
            }
            else
                result += a1;

            if (a2 > curtime)
            {
                result += curtime;
                curtime++;
            }
            else
                result += a2;
        }

        cout << result << "\n";
    }

    return 0;
}