#include <bits/stdc++.h> #define ll long long #define str string #define pii pair<int, int> #define pll pair<ll, ll> #define fi first #define se second #define vc vector<char> #define vvc vector<vc> #define vi vector<int> #define vvi vector<vi> #define vvvi vector<vvi> #define vvvvi vector<vvvi> #define vll vector<ll> #define vvll vector<vll> #define vvvll vector<vvll> #define vvvvll vector<vvvll> #define vs vector<str> #define vvs vector<vs> #define vpii vector<pii> #define vvpii vector<vpii> #define vpll vector<pll> #define vvpll vector<vpll> #define vb vector<bool> #define vvb vector<vb> #define rep(i, a, b) for (int i = (a); i < int(b); i++) #define repi(i, a, b) for (int i = (a); i <= int(b); i++) using namespace std; int inf = 1e7; void solve() { int n; cin >> n; str s; cin >> s; vpii sticks; int ix = 0; while (ix < n && s[ix] == '0') { ix++; } if (ix == n) { cout << 0 << '\n'; return; } sticks.emplace_back(ix, inf); int jx = 0; while (n - 1 - jx >= 0 && s[n - 1 - jx] == '0') { jx++; } sticks.emplace_back(jx, inf); int i = ix; while (i <= n - 1 - jx) { if (s[i] == '1') { i++; continue; } int j = i; while (j <= n - 1 - jx && s[j] == '0') { j++; } int cnt = j - i; sticks.emplace_back(cnt - (cnt / 2), cnt / 2); i = j; } /*for (auto & [x, y] : sticks) { cerr << x << " " << y << endl; }*/ sort(sticks.begin(), sticks.end(), greater<>()); int k = 0; int ans = 0; for (auto p : sticks) { int x = p.fi; int y = p.se; if (y == inf) y = 0; if (x - k <= 0) { break; } if (x - k == 1 && y - k == 0) { ans++; k++; continue; } if (x - k == 1 && y - k == 1) { ans++; k++; continue; } if (x - k == 2 && y - k == 1) { ans += 2; k += 2; continue; } ans += x - k; k++; if (y - k > 0) { ans += y - k; k++; } } cout << n - ans << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); bool _multipleTestCases = true; if (_multipleTestCases) { ll t; cin >> t; while (t--) solve(); } else { 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 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 135 136 137 138 139 140 141 142 143 144 145 | #include <bits/stdc++.h> #define ll long long #define str string #define pii pair<int, int> #define pll pair<ll, ll> #define fi first #define se second #define vc vector<char> #define vvc vector<vc> #define vi vector<int> #define vvi vector<vi> #define vvvi vector<vvi> #define vvvvi vector<vvvi> #define vll vector<ll> #define vvll vector<vll> #define vvvll vector<vvll> #define vvvvll vector<vvvll> #define vs vector<str> #define vvs vector<vs> #define vpii vector<pii> #define vvpii vector<vpii> #define vpll vector<pll> #define vvpll vector<vpll> #define vb vector<bool> #define vvb vector<vb> #define rep(i, a, b) for (int i = (a); i < int(b); i++) #define repi(i, a, b) for (int i = (a); i <= int(b); i++) using namespace std; int inf = 1e7; void solve() { int n; cin >> n; str s; cin >> s; vpii sticks; int ix = 0; while (ix < n && s[ix] == '0') { ix++; } if (ix == n) { cout << 0 << '\n'; return; } sticks.emplace_back(ix, inf); int jx = 0; while (n - 1 - jx >= 0 && s[n - 1 - jx] == '0') { jx++; } sticks.emplace_back(jx, inf); int i = ix; while (i <= n - 1 - jx) { if (s[i] == '1') { i++; continue; } int j = i; while (j <= n - 1 - jx && s[j] == '0') { j++; } int cnt = j - i; sticks.emplace_back(cnt - (cnt / 2), cnt / 2); i = j; } /*for (auto & [x, y] : sticks) { cerr << x << " " << y << endl; }*/ sort(sticks.begin(), sticks.end(), greater<>()); int k = 0; int ans = 0; for (auto p : sticks) { int x = p.fi; int y = p.se; if (y == inf) y = 0; if (x - k <= 0) { break; } if (x - k == 1 && y - k == 0) { ans++; k++; continue; } if (x - k == 1 && y - k == 1) { ans++; k++; continue; } if (x - k == 2 && y - k == 1) { ans += 2; k += 2; continue; } ans += x - k; k++; if (y - k > 0) { ans += y - k; k++; } } cout << n - ans << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); bool _multipleTestCases = true; if (_multipleTestCases) { ll t; cin >> t; while (t--) solve(); } else { solve(); } return 0; } |