#include <bits/stdc++.h> using namespace std; long long z; vector<pair<long long, long long> > strefy; bool por(pair<long long, long long> a, pair<long long, long long> b){ if(a.first == b.first){ if(a.second<b.second){ return false; } }else{ return a>b; } } void solve(){ long long n; string s; cin>>n>>s; long long d = 0; bool l = false; if(s[0]=='1') l = true; for(char c : s){ if(c=='1' && d!=0){ if(l){ strefy.push_back({d-1, d-1}); }else{ strefy.push_back({2*d, d}); l = true; } d=0; }else if(c=='0'){ d++; } } if(d!=0){ strefy.push_back({2*d, d}); } sort(strefy.begin(),strefy.end(), por); long long ruchy = 0, xd = 0; for(auto x : strefy){ if(x.first == x.second){ if(x.second-(ruchy*2)==0){ n--; ruchy++; }else{ n-=max(xd,x.second-(ruchy*2)); ruchy+=2; } }else{ n-=max(xd, x.second-ruchy); ruchy++; } } cout<<n<<"\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>z; for(int i = 0; i<z;i++){ solve(); strefy.clear(); } return 0; }
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 | #include <bits/stdc++.h> using namespace std; long long z; vector<pair<long long, long long> > strefy; bool por(pair<long long, long long> a, pair<long long, long long> b){ if(a.first == b.first){ if(a.second<b.second){ return false; } }else{ return a>b; } } void solve(){ long long n; string s; cin>>n>>s; long long d = 0; bool l = false; if(s[0]=='1') l = true; for(char c : s){ if(c=='1' && d!=0){ if(l){ strefy.push_back({d-1, d-1}); }else{ strefy.push_back({2*d, d}); l = true; } d=0; }else if(c=='0'){ d++; } } if(d!=0){ strefy.push_back({2*d, d}); } sort(strefy.begin(),strefy.end(), por); long long ruchy = 0, xd = 0; for(auto x : strefy){ if(x.first == x.second){ if(x.second-(ruchy*2)==0){ n--; ruchy++; }else{ n-=max(xd,x.second-(ruchy*2)); ruchy+=2; } }else{ n-=max(xd, x.second-ruchy); ruchy++; } } cout<<n<<"\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>z; for(int i = 0; i<z;i++){ solve(); strefy.clear(); } return 0; } |