#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; #define st first #define nd second typedef pair<int,int> pun; typedef long long ll; int test() { int n; string s; cin >> n >> s; int pref = 0; int suf = 0; while (pref < n && s[pref] == '0') pref ++; while (suf < n && s[n-suf-1] == '0') suf ++; if (pref == n) return 0; int res = 0; vector<int> breaks; breaks.push_back(pref); breaks.push_back(suf); int x = 0; for (int i = pref; i < n - suf; i ++) { if (s[i] == '1') { breaks.push_back(x / 2); breaks.push_back((x+1) / 2); x = 0; } else x++; } breaks.push_back(x); sort(breaks.begin(), breaks.end()); reverse(breaks.begin(), breaks.end()); int y = 0; for (int x : breaks) { if (x > y) { res += x - y; y++; } } return n-res; } int main() { ios_base::sync_with_stdio(false); int t; cin >> t; for (int i = 0; i < t; i ++) { cout << test() <<"\n"; } }
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 | #include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; #define st first #define nd second typedef pair<int,int> pun; typedef long long ll; int test() { int n; string s; cin >> n >> s; int pref = 0; int suf = 0; while (pref < n && s[pref] == '0') pref ++; while (suf < n && s[n-suf-1] == '0') suf ++; if (pref == n) return 0; int res = 0; vector<int> breaks; breaks.push_back(pref); breaks.push_back(suf); int x = 0; for (int i = pref; i < n - suf; i ++) { if (s[i] == '1') { breaks.push_back(x / 2); breaks.push_back((x+1) / 2); x = 0; } else x++; } breaks.push_back(x); sort(breaks.begin(), breaks.end()); reverse(breaks.begin(), breaks.end()); int y = 0; for (int x : breaks) { if (x > y) { res += x - y; y++; } } return n-res; } int main() { ios_base::sync_with_stdio(false); int t; cin >> t; for (int i = 0; i < t; i ++) { cout << test() <<"\n"; } } |