#include<bits/stdc++.h> using namespace std; const int N = 100 * 1000 + 7; vector<pair<int, int>> v; void Solve() { int n, il, c, w; cin >> n; c = 0; il = 0; w = 0; for(int i = 1; i <= n; ++i) { char x; cin >> x; if(x == '1') { if(c == 0) v.push_back(make_pair(il * 2, 1)); else v.push_back(make_pair(il, 2)); c = 1; il = 0; }else { ++il; } } if(il != 0) v.push_back(make_pair(il * 2, 1)); sort(v.begin(), v.end()); reverse(v.begin(), v.end()); il = 0; for(int i = 0; i < (int)v.size(); ++i) { if(il * 2 >= v[i].first) break; if(v[i].second == 1) { w += v[i].first / 2 - il; ++il; }else { w += v[i].first - il * 2 - 1; if(v[i].first - il * 2 == 1) ++w; il += 2; } } v.clear(); cout << n - w << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; 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 | #include<bits/stdc++.h> using namespace std; const int N = 100 * 1000 + 7; vector<pair<int, int>> v; void Solve() { int n, il, c, w; cin >> n; c = 0; il = 0; w = 0; for(int i = 1; i <= n; ++i) { char x; cin >> x; if(x == '1') { if(c == 0) v.push_back(make_pair(il * 2, 1)); else v.push_back(make_pair(il, 2)); c = 1; il = 0; }else { ++il; } } if(il != 0) v.push_back(make_pair(il * 2, 1)); sort(v.begin(), v.end()); reverse(v.begin(), v.end()); il = 0; for(int i = 0; i < (int)v.size(); ++i) { if(il * 2 >= v[i].first) break; if(v[i].second == 1) { w += v[i].first / 2 - il; ++il; }else { w += v[i].first - il * 2 - 1; if(v[i].first - il * 2 == 1) ++w; il += 2; } } v.clear(); cout << n - w << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while(t--) Solve(); return 0; } |