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
#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>

using namespace std;

int lowest (const void * a, const void * b) {
    return ( *(int*)a - *(int*)b );
}

void random_shuffle( const vector<int, allocator<int>>::iterator first, const vector<int, allocator<int>>::iterator last )
{
    int n = last - first;
    for (int i = 0; i < n; i++) {
        swap(first[i], first[rand() % n]);
    }
}

class MyStack: public stack<int, vector<int>> {
public:
    void clear() { c.clear(); }
};

vector<int> v;
MyStack oneWay;
MyStack twoWay;

int main() {

    int t, n;
    string string;

    cin >> t;
    for (int tt = 0; tt < t; tt++) {
        cin >> n;
        cin >> string;

        v.clear();
        oneWay.clear();
        twoWay.clear();

        bool border = string[0] == '0';
        int currentLen = 0;
        for (char const &c: string) {
            if (c == '0') {
                currentLen++;
            } else {
                if (currentLen > 0) {
                    if (border) {
                        border = false;
                        oneWay.push(currentLen);
                    } else {
                        v.push_back(currentLen);
                    }
                    currentLen = 0;
                }
            }
        }
        
        if(currentLen > 0) {
            if(oneWay.empty()) {
                oneWay.push(currentLen);
            } else {
                int top = oneWay.top();
                if(top > currentLen) {
                    oneWay.pop();
                    oneWay.push(currentLen);
                    oneWay.push(top);
                } else {
                    oneWay.push(currentLen);
                }
            }
        }
        
        random_shuffle(v.begin(), v.end());
        qsort(&v[0], v.size(), sizeof(int), lowest);

        for (int const &i: v) {
            twoWay.push(i);
        }

        int result = 0;
        int currentShift = 0;
        for(;;) {
            int k1 = oneWay.empty() ? 0 : oneWay.top();
            int origM2 = twoWay.empty() ? 0 : twoWay.top();

            k1 -= currentShift;
            int m2 = origM2 - 2*currentShift;

            if(k1 <= 0 && m2 <= 0) {
                break;
            }

            if(k1 >= (m2+1)/2) {
                oneWay.pop();
                result += k1;
            } else {
                twoWay.pop();
                oneWay.push(origM2-currentShift-1);
                result++;
            }

            currentShift++;
        }

        cout << n-result << endl;
    }

    return 0;
}