#include <bits/stdc++.h> #define ll long long #define mp make_pair #define fi first #define se second #define pb push_back #define vi vector<int> #define pi pair<int, int> #define mod 998244353 template<typename T> bool chkmin(T &a, T b){return (b < a) ? a = b, 1 : 0;} template<typename T> bool chkmax(T &a, T b){return (b > a) ? a = b, 1 : 0;} ll ksm(ll a, ll b) {if (b == 0) return 1; ll ns = ksm(a, b >> 1); ns = ns * ns % mod; if (b & 1) ns = ns * a % mod; return ns;} using namespace std; int n; const int maxn = 500005; char inp[maxn]; int cal(vi a, int b) { int ans = 0; for (int i = 0; i < a.size(); i++) { int lst = a[i] - 2 * b; if (lst <= 0) continue; ans += 1, b += 1, lst -= 2; if (lst > 0) ans += lst, b += 1; } return ans; } int main() { int t; cin >> t; while (t--) { int n; cin >> n; scanf("%s", inp + 1); vi tt; tt.pb(0); for (int i = 1; i <= n; i++) if (inp[i] == '1') tt.pb(i); tt.pb(n + 1); int ans = 0; if (tt.size() == 2) ans = 0; else { vi f, e; f.pb(tt[1] - tt[0] - 1); f.pb(tt[tt.size() - 1] - tt[tt.size() - 2] - 1); for (int i = 1; i < tt.size() - 2; i++) e.pb(tt[i + 1] - tt[i] - 1); sort(e.begin(), e.end()); reverse(e.begin(), e.end()); int mx = 0; for (int m = 0; m < 4; m++) { int st = 0; int cans = 0; for (int j = 0; j < 2; j++) if (m & (1 << j)) { cans += (f[j] - st); st += 1; } cans += cal(e, st); chkmax(mx, cans); } ans = n - mx; } printf("%d\n", ans); } return (0-0); // <3 cxr }
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 | #include <bits/stdc++.h> #define ll long long #define mp make_pair #define fi first #define se second #define pb push_back #define vi vector<int> #define pi pair<int, int> #define mod 998244353 template<typename T> bool chkmin(T &a, T b){return (b < a) ? a = b, 1 : 0;} template<typename T> bool chkmax(T &a, T b){return (b > a) ? a = b, 1 : 0;} ll ksm(ll a, ll b) {if (b == 0) return 1; ll ns = ksm(a, b >> 1); ns = ns * ns % mod; if (b & 1) ns = ns * a % mod; return ns;} using namespace std; int n; const int maxn = 500005; char inp[maxn]; int cal(vi a, int b) { int ans = 0; for (int i = 0; i < a.size(); i++) { int lst = a[i] - 2 * b; if (lst <= 0) continue; ans += 1, b += 1, lst -= 2; if (lst > 0) ans += lst, b += 1; } return ans; } int main() { int t; cin >> t; while (t--) { int n; cin >> n; scanf("%s", inp + 1); vi tt; tt.pb(0); for (int i = 1; i <= n; i++) if (inp[i] == '1') tt.pb(i); tt.pb(n + 1); int ans = 0; if (tt.size() == 2) ans = 0; else { vi f, e; f.pb(tt[1] - tt[0] - 1); f.pb(tt[tt.size() - 1] - tt[tt.size() - 2] - 1); for (int i = 1; i < tt.size() - 2; i++) e.pb(tt[i + 1] - tt[i] - 1); sort(e.begin(), e.end()); reverse(e.begin(), e.end()); int mx = 0; for (int m = 0; m < 4; m++) { int st = 0; int cans = 0; for (int j = 0; j < 2; j++) if (m & (1 << j)) { cans += (f[j] - st); st += 1; } cans += cal(e, st); chkmax(mx, cans); } ans = n - mx; } printf("%d\n", ans); } return (0-0); // <3 cxr } |