#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef pair<ld, ld> PLD; typedef vector<int> VI; typedef vector<ll> VL; typedef vector<PII> VII; typedef vector<VI> VVI; typedef vector<VL> VVL; typedef vector<VII> VVII; typedef vector<PLL> VLL; typedef vector<VLL> VVLL; typedef vector<VI> VVI; typedef vector<VL> VVL; typedef vector<bool> VB; template <class T, class G> ostream &operator<<(ostream &os, const pair<T, G> ¶) { os << para.first << " " << para.second; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) { for (const T &el : vec) os << el << " "; return os; } template <class T> ostream &operator<<(ostream &os, const set<T> &vec) { for (const T &el : vec) os << el << " "; return os; } #define REP(i, j) for (int i = 0; i < j; i++) #define REP1(i, j) for (int i = 1; i < j; i++) #define FOREACH(el, n) for (auto &el : n) #define LAST(x) (x[(int)x.size() - 1]) #define pb push_back #define pf push_front #define st first #define nd second #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define sp ' ' #define ent '\n' int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int t = 1; cin >> t; while (t--) { int n; string s; cin >> n >> s; bool fd = false; priority_queue<pair<PII, int>> pq; int cnt = 0; REP(i, n) { if (s[i] == '0') { cnt++; } else { if (i == 0) fd = true; if (cnt == 0) continue; if (fd) { pq.push({{(cnt + 1) / 2, -2}, cnt}); } else { pq.push({{cnt, -1}, 0}); fd = true; } cnt = 0; } } if (cnt > 0) { pq.push({{cnt, -1}, 0}); } if (!fd) { cout << 0 << ent; continue; } int saved = 0; int days_passed = 0; while (!pq.empty()) { auto v = pq.top(); pq.pop(); // cout << v << ent; if (v.st.st <= days_passed) break; if (v.st.nd == -2) { int oryg_len = v.nd; v.st.nd = -1; v.nd = 0; v.st.st = oryg_len - days_passed - 1; pq.push(v); saved++; } else { saved += v.st.st - days_passed; } days_passed++; } cout << n - saved << ent; } 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 105 106 107 108 109 110 111 112 113 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef pair<ld, ld> PLD; typedef vector<int> VI; typedef vector<ll> VL; typedef vector<PII> VII; typedef vector<VI> VVI; typedef vector<VL> VVL; typedef vector<VII> VVII; typedef vector<PLL> VLL; typedef vector<VLL> VVLL; typedef vector<VI> VVI; typedef vector<VL> VVL; typedef vector<bool> VB; template <class T, class G> ostream &operator<<(ostream &os, const pair<T, G> ¶) { os << para.first << " " << para.second; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) { for (const T &el : vec) os << el << " "; return os; } template <class T> ostream &operator<<(ostream &os, const set<T> &vec) { for (const T &el : vec) os << el << " "; return os; } #define REP(i, j) for (int i = 0; i < j; i++) #define REP1(i, j) for (int i = 1; i < j; i++) #define FOREACH(el, n) for (auto &el : n) #define LAST(x) (x[(int)x.size() - 1]) #define pb push_back #define pf push_front #define st first #define nd second #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define sp ' ' #define ent '\n' int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int t = 1; cin >> t; while (t--) { int n; string s; cin >> n >> s; bool fd = false; priority_queue<pair<PII, int>> pq; int cnt = 0; REP(i, n) { if (s[i] == '0') { cnt++; } else { if (i == 0) fd = true; if (cnt == 0) continue; if (fd) { pq.push({{(cnt + 1) / 2, -2}, cnt}); } else { pq.push({{cnt, -1}, 0}); fd = true; } cnt = 0; } } if (cnt > 0) { pq.push({{cnt, -1}, 0}); } if (!fd) { cout << 0 << ent; continue; } int saved = 0; int days_passed = 0; while (!pq.empty()) { auto v = pq.top(); pq.pop(); // cout << v << ent; if (v.st.st <= days_passed) break; if (v.st.nd == -2) { int oryg_len = v.nd; v.st.nd = -1; v.nd = 0; v.st.st = oryg_len - days_passed - 1; pq.push(v); saved++; } else { saved += v.st.st - days_passed; } days_passed++; } cout << n - saved << ent; } return 0; } |