#include <bits/stdc++.h> #define st first #define nd second #define pb push_back #define BOOST ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); #define PI 3.14159265359 using namespace std; typedef long long ll; constexpr ll MOD = 1000000007, sup = 29; constexpr ll MOD_1 = 1234567891, sup_1 = 1009; constexpr ll MOD_2 = 1000000009, sup_2 = 107; constexpr int MXN = 100000+10, CZAPA = (1<<19)/*, INF = 1000000000*/; constexpr ll INF = 1000000000000000000; priority_queue<int> A, B; int main(){ BOOST; int queries; cin>> queries; while(queries--){ while(!A.empty()) A.pop(); while(!B.empty()) B.pop(); int n; string s; cin>> n >> s; bool cool = true; for(char i : s){ if(i == '1'){ cool = false; break; } } if(cool){ cout<< 0 << '\n'; continue; } for(int i=0; i<n; i++){ if(s[i] == '1') continue; int j = i; while(j+1 < n && s[j+1] == '0') j++; if(i == 0 || j == n-1){ B.push(j-i+1); } else{ A.push(j-i+1); } } int ans = 0, t = 0; while(!A.empty() && !B.empty()){ int a = 0, b = 0; if(!A.empty()){ a = A.top(); } if(!B.empty()){ b = B.top(); } a -= 2*t; b -= t; if(a <= 0 && b <= 0) break; //cout<< a << ' ' << b << ' '; if(a > b){ //cout<< "A\n"; a += t-1; ans++; A.pop(); B.push(a); b += t; } else{ //cout<< "B\n"; B.pop(); ans += b; } t++; } cout<< n-ans << '\n'; } 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 | #include <bits/stdc++.h> #define st first #define nd second #define pb push_back #define BOOST ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); #define PI 3.14159265359 using namespace std; typedef long long ll; constexpr ll MOD = 1000000007, sup = 29; constexpr ll MOD_1 = 1234567891, sup_1 = 1009; constexpr ll MOD_2 = 1000000009, sup_2 = 107; constexpr int MXN = 100000+10, CZAPA = (1<<19)/*, INF = 1000000000*/; constexpr ll INF = 1000000000000000000; priority_queue<int> A, B; int main(){ BOOST; int queries; cin>> queries; while(queries--){ while(!A.empty()) A.pop(); while(!B.empty()) B.pop(); int n; string s; cin>> n >> s; bool cool = true; for(char i : s){ if(i == '1'){ cool = false; break; } } if(cool){ cout<< 0 << '\n'; continue; } for(int i=0; i<n; i++){ if(s[i] == '1') continue; int j = i; while(j+1 < n && s[j+1] == '0') j++; if(i == 0 || j == n-1){ B.push(j-i+1); } else{ A.push(j-i+1); } } int ans = 0, t = 0; while(!A.empty() && !B.empty()){ int a = 0, b = 0; if(!A.empty()){ a = A.top(); } if(!B.empty()){ b = B.top(); } a -= 2*t; b -= t; if(a <= 0 && b <= 0) break; //cout<< a << ' ' << b << ' '; if(a > b){ //cout<< "A\n"; a += t-1; ans++; A.pop(); B.push(a); b += t; } else{ //cout<< "B\n"; B.pop(); ans += b; } t++; } cout<< n-ans << '\n'; } return 0; } |