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

using namespace std;

const int maxN = 100000;

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

  int t, n, first, last, curr;
  cin >> t;
  char c;

  while (t-- > 0) {
    cin >> n;
    first = last = -1;
    curr = 0;
    vector<int> v;

    for(int i=0; i<n; i++){
      cin>>c;
      if(c == '1'){
        if(first == -1){
          first = curr;
        }
        else{
          if(curr > 0)
            v.push_back(curr);
        }
        curr = 0;
      } else {
        curr++;
      }
    }
    last = curr;

    sort(v.begin(), v.end());


    int result =0;
    int time = 0;



//    cout<<"line nr: "<<t<<", first: "<<first<<", last: "<< last <<", inside: ";
//    for(int i=0; i<v.size(); i++)
//      cout<<v[i]<<", ";

    for(int i = v.size() - 1; i > 0; i--){

      if(first > v[i] / 2  && first > time){
        result += first - time;
        first = -1;
        time ++;
      }
      if(last > v[i] / 2  && last > time){
        result += last - time;
        last = -1;
        time ++;
      }

      if(v[i] - 2 * time - 1 <= 0)
        break;
      result += max(0, v[i] - 2 * time - 1);
      time += 2;
    }

    if(last > time){
      result += last - time;
      time ++;
    }

    if(first > time){
      result += first - time;
      time ++;
    }

//    cout<<"   -->>  "<< n - result <<"\n";
//    cout<<"\n";
    cout<<n-result<<"\n";
  }
}