#define pb push_back #define mp make_pair #define fi first #define se second #define all(...) begin(__VA_ARGS__) , end(__VA_ARGS__) #define boost {ios_base::sync_with_stdio(false); cin.tie(); cout.tie();} #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector <int> vi; typedef pair<int,int> PII; typedef pair<ll,ll> PLL; constexpr ll nax = 5e5+6969, INF = 2e9+6969; int n; void test_case() { scanf("%d", &n); string S; cin >> S; vi A, B; bool First = 1; int cnt = 0; if(S[0] == '1') First = 0; for(int i=0;i<n;i++) { if(S[i] == '1') { if(cnt == 0) continue; if(First) { First = 0; A.pb(cnt); } else B.pb(cnt); cnt = 0; continue; } cnt++; } if(S[n - 1] == '0') { A.pb(cnt); } sort(all(B)); sort(all(A)); reverse(all(A)); reverse(all(B)); int pos = 0, ans = 0, rem = 0; for(int i=0;i<B.size();i++) { if(rem * 2 >= B[i]) continue; B[i] -= rem * 2; while(pos < A.size()) { if(A[pos] <= rem) { pos++; continue; } if(B[i] == 1) { B[i] = 0; ans+=A[pos]-rem; pos++; rem++; break; } else if(B[i] == 2) { B[i] = 0; ans+=A[pos]-rem; pos++; rem++; break; } else if(B[i] == 3) { ans += A[pos] + 1-rem; rem += 2; B[i] = 0; pos++; break; } else if(B[i] == 4) { ans += A[pos] + 2-rem; pos++; rem += 3; B[i] = 0; break; } else { ans += A[pos] - rem; rem++; B[i] -= 2; pos++; } } if(B[i] > 0) { ans += max(1, B[i] - 1); if(B[i] == 1) rem++; else rem += 2; } } while(pos < A.size()) { if(rem >= A[pos]) { pos++; continue; } ans += A[pos] - rem; rem++; pos++; } printf("%d\n", n - ans); } int main() { int qq; scanf("%d", &qq); while(qq--) test_case(); 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 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 127 128 129 130 131 132 133 134 | #define pb push_back #define mp make_pair #define fi first #define se second #define all(...) begin(__VA_ARGS__) , end(__VA_ARGS__) #define boost {ios_base::sync_with_stdio(false); cin.tie(); cout.tie();} #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef vector <int> vi; typedef pair<int,int> PII; typedef pair<ll,ll> PLL; constexpr ll nax = 5e5+6969, INF = 2e9+6969; int n; void test_case() { scanf("%d", &n); string S; cin >> S; vi A, B; bool First = 1; int cnt = 0; if(S[0] == '1') First = 0; for(int i=0;i<n;i++) { if(S[i] == '1') { if(cnt == 0) continue; if(First) { First = 0; A.pb(cnt); } else B.pb(cnt); cnt = 0; continue; } cnt++; } if(S[n - 1] == '0') { A.pb(cnt); } sort(all(B)); sort(all(A)); reverse(all(A)); reverse(all(B)); int pos = 0, ans = 0, rem = 0; for(int i=0;i<B.size();i++) { if(rem * 2 >= B[i]) continue; B[i] -= rem * 2; while(pos < A.size()) { if(A[pos] <= rem) { pos++; continue; } if(B[i] == 1) { B[i] = 0; ans+=A[pos]-rem; pos++; rem++; break; } else if(B[i] == 2) { B[i] = 0; ans+=A[pos]-rem; pos++; rem++; break; } else if(B[i] == 3) { ans += A[pos] + 1-rem; rem += 2; B[i] = 0; pos++; break; } else if(B[i] == 4) { ans += A[pos] + 2-rem; pos++; rem += 3; B[i] = 0; break; } else { ans += A[pos] - rem; rem++; B[i] -= 2; pos++; } } if(B[i] > 0) { ans += max(1, B[i] - 1); if(B[i] == 1) rem++; else rem += 2; } } while(pos < A.size()) { if(rem >= A[pos]) { pos++; continue; } ans += A[pos] - rem; rem++; pos++; } printf("%d\n", n - ans); } int main() { int qq; scanf("%d", &qq); while(qq--) test_case(); return 0; } |