#include <bits/stdc++.h> using namespace std; #define pb push_back #define st first #define nd second vector <pair <int, bool> > v; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t, n, ost, ld, ans; string s; cin >> t; for(int j = 0; j < t; j++){ cin >> n; cin >> s; ost = 0; for(int i = 0; i < n; i++){ if(s[i] == '1'){ if(ost == 0) v.pb({i*2, 1}); else v.pb({i - ost, 0}); ost = i+1; } } if(v.size()){ v.pb({2*(n - ost), 1}); sort(v.begin(), v.end(), greater<pair <int, bool> >()); ld = 0; ans = 0; //for(int i = 0; i < (int)v.size(); i++) cout << v[i].st << " " << v[i].nd << "\n"; for(int i = 0; i < (int)v.size(); i++){ if(v[i].nd) { v[i].st /= 2; v[i].st -= ld; if(v[i].st > 0){ ans += v[i].st; ld++; } } else{ v[i].st -= 2 * ld; if(v[i].st > 0) { ans += max(v[i].st - 1, 1); ld += 2; } } } cout << n - ans << "\n"; } else cout << 0 << "\n"; v.clear(); } 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 | #include <bits/stdc++.h> using namespace std; #define pb push_back #define st first #define nd second vector <pair <int, bool> > v; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t, n, ost, ld, ans; string s; cin >> t; for(int j = 0; j < t; j++){ cin >> n; cin >> s; ost = 0; for(int i = 0; i < n; i++){ if(s[i] == '1'){ if(ost == 0) v.pb({i*2, 1}); else v.pb({i - ost, 0}); ost = i+1; } } if(v.size()){ v.pb({2*(n - ost), 1}); sort(v.begin(), v.end(), greater<pair <int, bool> >()); ld = 0; ans = 0; //for(int i = 0; i < (int)v.size(); i++) cout << v[i].st << " " << v[i].nd << "\n"; for(int i = 0; i < (int)v.size(); i++){ if(v[i].nd) { v[i].st /= 2; v[i].st -= ld; if(v[i].st > 0){ ans += v[i].st; ld++; } } else{ v[i].st -= 2 * ld; if(v[i].st > 0) { ans += max(v[i].st - 1, 1); ld += 2; } } } cout << n - ans << "\n"; } else cout << 0 << "\n"; v.clear(); } return 0; } |