#include <bits/stdc++.h> using namespace std; int calc(const vector<int> &z,int day) { int sum = 0; for (int i=0;i<z.size();i++) { int saved = z[i]-day*2; if (saved <= 0) break; sum += max(1,saved-1); day += 2; } return sum; } int main() { ios_base::sync_with_stdio(0); int t; cin >> t; while (t--) { int n; cin >> n; string s; cin >> s; vector<int> z[2]; int d=0,f=0; for (int i=0;i<n;i++) { if (s[i]=='0') d++; else { if (d>0) { z[f].push_back(d); d=0; } f=1; } } if (d>0) z[0].push_back(d); for (int i=0;i<2;i++) sort(z[i].begin(),z[i].end(),greater<int>()); int ans = calc(z[1],0); int s0 = 0; for (int day=0;day<z[0].size();day++) { s0 += z[0][day]-day; ans = max(ans, s0 + calc(z[1],day+1)); } cout << n-ans << endl; } 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 | #include <bits/stdc++.h> using namespace std; int calc(const vector<int> &z,int day) { int sum = 0; for (int i=0;i<z.size();i++) { int saved = z[i]-day*2; if (saved <= 0) break; sum += max(1,saved-1); day += 2; } return sum; } int main() { ios_base::sync_with_stdio(0); int t; cin >> t; while (t--) { int n; cin >> n; string s; cin >> s; vector<int> z[2]; int d=0,f=0; for (int i=0;i<n;i++) { if (s[i]=='0') d++; else { if (d>0) { z[f].push_back(d); d=0; } f=1; } } if (d>0) z[0].push_back(d); for (int i=0;i<2;i++) sort(z[i].begin(),z[i].end(),greater<int>()); int ans = calc(z[1],0); int s0 = 0; for (int day=0;day<z[0].size();day++) { s0 += z[0][day]-day; ans = max(ans, s0 + calc(z[1],day+1)); } cout << n-ans << endl; } return 0; } |