#include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() #define debug std::cout << "hmm" << std::endl; typedef long long ll; typedef std::pair<int, int> pii; typedef std::pair<ll, ll> pll; typedef std::vector<int> vi; typedef std::vector<ll> vl; template <typename T> void print(T t) { std::cout << t << ' '; } template <typename T1, typename T2> void print(std::pair<T1, T2> t) { std::cout << '{' << t.first << ' ' << t.second << '}'; } template <typename T> void print(std::vector<T> t) { for (auto a : t) print(a); std::cout << std::endl; } void solve(int &ans, int t, vi &V) { int it = 0; while (it < V.size() && V[it] - t * 2 > 0) { if (V[it] - t * 2 == 1) ans++; else ans += V[it] - t * 2 - 1; t += 2; it++; } } main() { std::cin.tie(0)->sync_with_stdio(0); std::cin.exceptions(std::cin.failbit); int Z = 1; std::cin >> Z; while (Z--) { int n; std::cin >> n; std::string S; std::cin >> S; vi V(1, 0); for (int i = 0; i < n; i++) if (S[i] == '0') V.back()++; else V.push_back(0); if (V.size() == 1) { if (V.back()) std::cout << 0 << std::endl; else std::cout << n << std::endl; continue; } int end = V.back(); int start = V[0]; for (int i = 0; i < V.size() - 1; i++) V[i] = V[i + 1]; V.resize(V.size() - 2); if (V.size()) { std::sort(V.begin(), V.end()); std::reverse(V.begin(), V.end()); } int ans1 = 0, ans2 = 0, ans3 = 0; if (start < end) std::swap(start, end); if (start) { if (end && start + end > 2) { ans1 = start + end - 1; solve(ans1, 2, V); } ans2 = start; solve(ans2, 1, V); } solve(ans3, 0, V); std::cout << n - std::max(std::max(ans1, ans2), ans3) << std::endl; } }
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | #include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() #define debug std::cout << "hmm" << std::endl; typedef long long ll; typedef std::pair<int, int> pii; typedef std::pair<ll, ll> pll; typedef std::vector<int> vi; typedef std::vector<ll> vl; template <typename T> void print(T t) { std::cout << t << ' '; } template <typename T1, typename T2> void print(std::pair<T1, T2> t) { std::cout << '{' << t.first << ' ' << t.second << '}'; } template <typename T> void print(std::vector<T> t) { for (auto a : t) print(a); std::cout << std::endl; } void solve(int &ans, int t, vi &V) { int it = 0; while (it < V.size() && V[it] - t * 2 > 0) { if (V[it] - t * 2 == 1) ans++; else ans += V[it] - t * 2 - 1; t += 2; it++; } } main() { std::cin.tie(0)->sync_with_stdio(0); std::cin.exceptions(std::cin.failbit); int Z = 1; std::cin >> Z; while (Z--) { int n; std::cin >> n; std::string S; std::cin >> S; vi V(1, 0); for (int i = 0; i < n; i++) if (S[i] == '0') V.back()++; else V.push_back(0); if (V.size() == 1) { if (V.back()) std::cout << 0 << std::endl; else std::cout << n << std::endl; continue; } int end = V.back(); int start = V[0]; for (int i = 0; i < V.size() - 1; i++) V[i] = V[i + 1]; V.resize(V.size() - 2); if (V.size()) { std::sort(V.begin(), V.end()); std::reverse(V.begin(), V.end()); } int ans1 = 0, ans2 = 0, ans3 = 0; if (start < end) std::swap(start, end); if (start) { if (end && start + end > 2) { ans1 = start + end - 1; solve(ans1, 2, V); } ans2 = start; solve(ans2, 1, V); } solve(ans3, 0, V); std::cout << n - std::max(std::max(ans1, ans2), ans3) << std::endl; } } |