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
#include <iostream>
#include <vector>
#include <string>
#include <map>



using namespace std;

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


    int t, n, ans;
    string s;
    map <int, int> M2, M1;
    cin >>t;
    for (int X=0; X<t; X++)
    {
        M1.clear();
        M2.clear();
        cin>>n>>s;
        int sus = 0, based = 0;
        int i, tempx=0;
        for (i = 0; i < n; i++)
        {
            if (s[i]=='0')
            {
                tempx++;
            }
            else
            {
                if (sus<2)
                    sus++;
                if (tempx>0)
                {
                    if (sus == 1)
                    {
                        M1[tempx]++;
                    }
                    else
                    {
                        M2[tempx]++;
                    }
                }
                tempx = 0;
            }
        }

        if (sus > 0)
        {
            if (tempx >0)
                M1[tempx]++;
        }
        else
        {
            cout<<0<<endl;
            continue;
        }
                                               //trwa bardzo kr�tko. Generuje zestawy otoczonych podw�jnie jak i pojedynczo

        int M1max;
        int M2max;

        while (!( M1.empty() && M2.empty() ))
        {
            auto M1max = (M1.empty()) ? 0 : M1.rbegin()->first;
            auto M2max = (M2.empty()) ? 0 : M2.rbegin()->first;
            if ((M1max*2)>=M2max)
            {
                based += (M1max);
                M1[M1max]--;
                if ((M1.rbegin()->second) <= 0)
                    M1.erase(M1max);
            }
            else
            {
                based++;
                M2[M2max]--;
                if(M2max>1)
                    M1[M2max-1]++;
                if ((M2.rbegin()->second) <= 0)
                    M2.erase(M2max);
            }                                                                   //algorytm szczepiacy



        map<int, int> M;
            for (auto it = M1.begin(); it != M1.end(); it++)            //algorytm zakazajacy
            {
                int y = it->first;
                if(y > 1)
                    M[y-1] += (it->second);
            }
            M1 = M;
            M.clear();
            for (auto it = M2.begin(); it != M2.end(); it++)
            {
                int y = it->first;
                if(y > 2)
                    M[y-2] += (it->second);
            }
            M2 = M;
            }
        cout << (n-based) << endl;
    }
    return 0;
}