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

vector<pair<int,bool>> breaks;

int main()
{
    string input;
    int t,n;
    cin>>t;
    for(int c=0;c<t;c++)
    {
        cin>>n;
        cin>>input;
        breaks.clear();
        char currentLetter='2';
        int currentBreak=0;
        bool firstBreak=false;
        for(int i=0;i<n;i++)
        {
            if(currentLetter!=input[i])
            {
                
                if(currentBreak!=0)
                    breaks.push_back(make_pair(currentBreak,firstBreak));
                currentBreak=0;
                if(currentLetter!='2')
                    firstBreak=true;
            }
            if(input[i]=='0')
                currentBreak++;
            currentLetter=input[i];
        }
        if(currentLetter=='0')
            breaks.push_back(make_pair(currentBreak,false));
        sort(breaks.begin(),breaks.end(),[](pair<int, bool> a, pair<int, bool>b){return (a.first)<(b.first) or (a.first==b.first and a.second==true and b.second==false);});
        int j=0;
        int saved=0;
        vector<pair<int,int>> halfStopped;
        vector<int> edges;
        bool addedExtraSavedForMiddleCity=false;
        int howManyEdges=0;
        //  if(c==469){
        for(int i=breaks.size()-1;i>=0;i--)
        {
            int howMuchToSave = breaks[i].first-2*j-1;
            if((howMuchToSave>=(int)halfStopped.size()*2) and breaks[i].second)
            {
                halfStopped.push_back(make_pair(breaks[i].first,j));
                //cerr<<"Added: "<<breaks[i].first<<" because "<<howMuchToSave<<">"<<2*halfStopped.size()<<" "<<(howMuchToSave>2*(int)halfStopped.size())<<endl;
            }
            else 
            if(breaks[i].second==false)
            {
                edges.push_back(breaks[i].first);
                //cerr<<"Added2: "<<breaks[i].first<<" because "<<howMuchToSave<<endl;
            }
            if(breaks[i].second==false)
                ;
            else
                j++;
        }
        int i=0;
        for(;i<halfStopped.size();i++)
        {
            //cerr<<"original size: "<<halfStopped[i].first<<" j: "<<halfStopped[i].second<<" halfstopped: "<<(int)halfStopped.size()<<" how many edges: "<<howManyEdges<<" i: "<<i<<endl;
            saved += max(0,halfStopped[i].first - halfStopped[i].second-((int)halfStopped.size())+howManyEdges-i); 
            //cerr<<"current saved:"<<saved<<endl;
            if(halfStopped[i].first - halfStopped[i].second-((int)halfStopped.size())+howManyEdges-i<=2 and halfStopped[i].first%2==1)
            {    
                saved++;
                addedExtraSavedForMiddleCity=true;
            }
            if(halfStopped[i].first - halfStopped[i].second-((int)halfStopped.size())+howManyEdges-i<=0)
            {
                break;
            }
        }
        if(addedExtraSavedForMiddleCity)
            i--;
        sort(edges.begin(),edges.end());
        //cerr<<"i: "<<i<<" halfStopped: "<<halfStopped.size()<<endl;;
        for(int j=((int)edges.size())-1;j>=0;j--)   
        {
            if(edges[j]-i-((int)halfStopped.size())>0)
            {
                //cerr<<"saved extra with edges: "<<edges[j]-i-((int)halfStopped.size())<<endl;;
                saved+=edges[j]-i-((int)halfStopped.size());
                i++;
            }
        }

        //cerr<<"edges size "<<edges.size() << "edges[0]"<<edges[0]<<" edges back "<< edges.back() << "half stopped size * 2: "<<(((int)halfStopped.size()))*2 << " halfStopped[0].first " << halfStopped.back().first << " last condition " << halfStopped.back().first-((((int)halfStopped.size())-1)*4)<<endl;

        if(edges.size()>0 and edges.back()>=(((int)halfStopped.size()))*2 and halfStopped.size()>0 and halfStopped.back().first % 2==1 and halfStopped.back().first-((((int)halfStopped.size())-1)*4)==3)
        {
            //cerr<<"extra one saved :)"<<endl;
            saved++;
            i++;
        }
        if(edges.size()>0 and edges.back()>=(((int)halfStopped.size()))*2 and halfStopped.size()>0 and halfStopped.back().first % 2==0 and halfStopped.back().first-((((int)halfStopped.size())-1)*4)==2)
        {
            //cerr<<"another one :)"<<endl;
            saved++;
            i++;
        }
        // if(edges.size()==2 and edges[0]>(((int)halfStopped.size())-1)*2+1 and edges.back()>(((int)halfStopped.size()))*2 and halfStopped.size()>0 and halfStopped.back().first % 2==0 and halfStopped.back().first-((((int)halfStopped.size())-1)*4)==4)
        // {
        //     //cerr<<"and another one :)"<<endl;
        //     saved++;
        //     i++;
        // }
        if(edges.size()==2 and edges[0]>(((int)halfStopped.size())-1)*2+1 and edges.back()>(((int)halfStopped.size()))*2 and edges[0]+edges.back()>(((int)halfStopped.size()))*2+1 and halfStopped.size()>0 and halfStopped.back().first % 2==1 and halfStopped.back().first-((((int)halfStopped.size())-1)*4)==5)
        {
            //cerr<<"and yet another one :)"<<endl;
            saved++;
            i++;
        }
        // for(int i=0;i<breaks.size();i++)
            //cerr<<breaks[i].first<<" ";
        //cerr<<endl;
        int result = n-saved;
        cout<<result<<endl;
        // }
    }
}