#include <bits/stdc++.h> //#pragma GCC optimize("Ofast") //#pragma GCC optimize ("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //szablon czesciowo skopiowany z profilu https://codeforces.com/profile/Geothermal using namespace std; typedef long long LL; typedef long double LD; typedef pair<int, int> pii; typedef pair<LL,LL> pll; typedef pair<LD,LD> pdd; typedef vector<int> vi; typedef vector<bool> vb; typedef vector<LD> vld; typedef vector<LL> vll; typedef vector<pii> vpii; typedef vector<pll> vpll; template<class T> using pq = priority_queue<T>; template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>; #define rep(i, a, b) for (int i=a; i<(b); i++) #define repd(i,a,b) for (int i = (a); i >= b; i--) #define sz(x) (int)(x).size() #define pb push_back #define st first #define nd second #define lb lower_bound #define ub upper_bound #define all(x) x.begin(), x.end() #define memo(x) memset(x, 0, sizeof(x)) #define debug(x) cerr << x << " " mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); LL losuj(LL a, LL b){ return a+rng()%(b-a+1);} void solve(){ int n; cin >> n; vi t(n); int zak=0; rep(i,0,n){ char c; cin >> c; t[i]=(int)(c-'0'); } pq<pii>Q; int cnt = 0, b=1; rep(i,0,n){ if(t[i]==0) cnt++; else{ if(b==1) Q.push({2*cnt, -b}); else Q.push({cnt,-b}); b=2; cnt=0; } } Q.push({2*cnt,-1}); int d=0, ans=0; while(!Q.empty()){ cnt = Q.top().st; b = -Q.top().nd; Q.pop(); //cout << cnt << " " << b << " " << d << " " << ans << "\n"; if(b==1) cnt/=2; cnt-=d*b; if(b==1 && cnt>0){ ans+=max(cnt,0); d++; } else if(b==2 && cnt==1){ d++; ans++; } else if(b==2 && cnt>1){ ans+=cnt-1; d+=2; } } cout << n-ans << "\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; cin >> t; while (t--) solve(); 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | #include <bits/stdc++.h> //#pragma GCC optimize("Ofast") //#pragma GCC optimize ("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //szablon czesciowo skopiowany z profilu https://codeforces.com/profile/Geothermal using namespace std; typedef long long LL; typedef long double LD; typedef pair<int, int> pii; typedef pair<LL,LL> pll; typedef pair<LD,LD> pdd; typedef vector<int> vi; typedef vector<bool> vb; typedef vector<LD> vld; typedef vector<LL> vll; typedef vector<pii> vpii; typedef vector<pll> vpll; template<class T> using pq = priority_queue<T>; template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>; #define rep(i, a, b) for (int i=a; i<(b); i++) #define repd(i,a,b) for (int i = (a); i >= b; i--) #define sz(x) (int)(x).size() #define pb push_back #define st first #define nd second #define lb lower_bound #define ub upper_bound #define all(x) x.begin(), x.end() #define memo(x) memset(x, 0, sizeof(x)) #define debug(x) cerr << x << " " mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); LL losuj(LL a, LL b){ return a+rng()%(b-a+1);} void solve(){ int n; cin >> n; vi t(n); int zak=0; rep(i,0,n){ char c; cin >> c; t[i]=(int)(c-'0'); } pq<pii>Q; int cnt = 0, b=1; rep(i,0,n){ if(t[i]==0) cnt++; else{ if(b==1) Q.push({2*cnt, -b}); else Q.push({cnt,-b}); b=2; cnt=0; } } Q.push({2*cnt,-1}); int d=0, ans=0; while(!Q.empty()){ cnt = Q.top().st; b = -Q.top().nd; Q.pop(); //cout << cnt << " " << b << " " << d << " " << ans << "\n"; if(b==1) cnt/=2; cnt-=d*b; if(b==1 && cnt>0){ ans+=max(cnt,0); d++; } else if(b==2 && cnt==1){ d++; ans++; } else if(b==2 && cnt>1){ ans+=cnt-1; d+=2; } } cout << n-ans << "\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; cin >> t; while (t--) solve(); return 0; } |