#include <iostream> #include <vector> #include <string> #include <cstring> #include <algorithm> #include <queue> using namespace std; void solve() { int n; cin >> n; priority_queue<pair<int, bool>> q; int ln = 0; char c, p = 0; bool first = true; for(int i = 0; i < n; i++) { cin >> c; if (c == '1') { if (ln > 0) { if (first) { ln *= 2; } q.push(make_pair(ln, first)); } first = false; ln = 0; } else if (c == '0') { ln++; } if (i == n-1 && ln > 0) { ln *= 2; q.push(make_pair(ln, true)); } } int dest = 0; int wyn = 0; int cln = 0; while(!q.empty()) { pair<int, bool> p = q.top(); q.pop(); int cw = 0; if (p.second == true) { cw = (p.first / 2) - dest; dest++; } else { cw = p.first - (dest * 2); if (cw > 1) { cw -= 1; } dest += 2; } if (cw > 0) { wyn += cw; } } cout << n - wyn << endl; } int main() { ios::sync_with_stdio(false); int t; cin >> t; while(t--) solve(); }
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 | #include <iostream> #include <vector> #include <string> #include <cstring> #include <algorithm> #include <queue> using namespace std; void solve() { int n; cin >> n; priority_queue<pair<int, bool>> q; int ln = 0; char c, p = 0; bool first = true; for(int i = 0; i < n; i++) { cin >> c; if (c == '1') { if (ln > 0) { if (first) { ln *= 2; } q.push(make_pair(ln, first)); } first = false; ln = 0; } else if (c == '0') { ln++; } if (i == n-1 && ln > 0) { ln *= 2; q.push(make_pair(ln, true)); } } int dest = 0; int wyn = 0; int cln = 0; while(!q.empty()) { pair<int, bool> p = q.top(); q.pop(); int cw = 0; if (p.second == true) { cw = (p.first / 2) - dest; dest++; } else { cw = p.first - (dest * 2); if (cw > 1) { cw -= 1; } dest += 2; } if (cw > 0) { wyn += cw; } } cout << n - wyn << endl; } int main() { ios::sync_with_stdio(false); int t; cin >> t; while(t--) solve(); } |