#if defined(EMBE_DEBUG) && !defined(NDEBUG) #include "embe-debug.hpp" #else #define LOG_INDENT(...) do {} while (false) #define LOG(...) do {} while (false) #define DUMP(...) do {} while (false) #endif #include <algorithm> #include <functional> #include <iostream> #include <optional> #include <string> #include <utility> #include <vector> using namespace std; namespace { vector<pair<int, int>> parse(string s) { vector<int> lens; { auto i = s.begin(); while (true) { auto j = find(i, s.end(), '1'); lens.push_back(j - i); if (j == s.end()) break; i = find(j, s.end(), '0'); } } int n = lens.size(); if (n == 1) { return {{0, lens[0]}}; } vector<pair<int, int>> res; res.reserve(lens.size()); if (lens.front() > 0) res.emplace_back(1, lens.front()); if (lens.back() > 0) res.emplace_back(1, lens.back()); for (int i = 1; i < n - 1; ++i) { res.emplace_back(2, lens[i]); } return res; } int solve(vector<pair<int, int>> data) { if (data.size() == 1 && data[0].first == 0) return data[0].second; vector<int> ones; vector<int> twos; for (auto const& [m, n]: data) { if (m == 1) ones.emplace_back(n); else twos.emplace_back(n); } sort(ones.begin(), ones.end(), greater<>()); sort(twos.begin(), twos.end(), greater<>()); int oo = ones.size(); optional<int> res; for (int o = 0; o <= oo; ++o) { int t = 0; int cur = 0; for (int i = 0; i < o; ++i) { int x = ones[i]; x -= t; if (x <= 0) break; cur += x; ++t; } for (auto x: twos) { x -= 2 * t; if (x <= 0) break; if (x <= 2) { ++cur; break; } cur += x - 1; t += 2; } if (!res || *res < cur) res = cur; } return *res; } } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t > 0) { --t; int n; string s; cin >> n >> s; auto tmp = parse(s); cout << n - solve(tmp) << 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 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 99 100 101 102 103 104 | #if defined(EMBE_DEBUG) && !defined(NDEBUG) #include "embe-debug.hpp" #else #define LOG_INDENT(...) do {} while (false) #define LOG(...) do {} while (false) #define DUMP(...) do {} while (false) #endif #include <algorithm> #include <functional> #include <iostream> #include <optional> #include <string> #include <utility> #include <vector> using namespace std; namespace { vector<pair<int, int>> parse(string s) { vector<int> lens; { auto i = s.begin(); while (true) { auto j = find(i, s.end(), '1'); lens.push_back(j - i); if (j == s.end()) break; i = find(j, s.end(), '0'); } } int n = lens.size(); if (n == 1) { return {{0, lens[0]}}; } vector<pair<int, int>> res; res.reserve(lens.size()); if (lens.front() > 0) res.emplace_back(1, lens.front()); if (lens.back() > 0) res.emplace_back(1, lens.back()); for (int i = 1; i < n - 1; ++i) { res.emplace_back(2, lens[i]); } return res; } int solve(vector<pair<int, int>> data) { if (data.size() == 1 && data[0].first == 0) return data[0].second; vector<int> ones; vector<int> twos; for (auto const& [m, n]: data) { if (m == 1) ones.emplace_back(n); else twos.emplace_back(n); } sort(ones.begin(), ones.end(), greater<>()); sort(twos.begin(), twos.end(), greater<>()); int oo = ones.size(); optional<int> res; for (int o = 0; o <= oo; ++o) { int t = 0; int cur = 0; for (int i = 0; i < o; ++i) { int x = ones[i]; x -= t; if (x <= 0) break; cur += x; ++t; } for (auto x: twos) { x -= 2 * t; if (x <= 0) break; if (x <= 2) { ++cur; break; } cur += x - 1; t += 2; } if (!res || *res < cur) res = cur; } return *res; } } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t > 0) { --t; int n; string s; cin >> n >> s; auto tmp = parse(s); cout << n - solve(tmp) << endl; } return 0; } |