#include <bits/stdc++.h> #define REP(i,n) for (int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for (int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for (int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) std::cerr << "TRACE(" #x ")" << std::endl; #define DEBUG(x) std::cerr << #x << " = " << (x) << std::endl; void init_io() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); } int solve_case() { int n; std::cin >> n; std::vector<int> count(2*n + 1, 0); bool leftmost = true; int len = 0; REP(i, n) { char c; std::cin >> c; if (c=='0') { ++len; } else if (c=='1') { if (leftmost) { count[2 * len] += 1; } else { count[len] += 2; } len = 0; leftmost = false; } else { assert(false); } } if (leftmost) { // no virus return 0; } count[2*len] += 1; int saved = 0; int water = 0; FORD(len, 2*n, 1) { REP(i, count[len]) { if (len <= water) goto done; saved += len - water; water += 2; } } done: return n - (saved + 1) / 2; } int main() { init_io(); int ntc; std::cin >> ntc; REP(tc, ntc) { int res = solve_case(); std::cout << res << '\n'; } }
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 | #include <bits/stdc++.h> #define REP(i,n) for (int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for (int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for (int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) std::cerr << "TRACE(" #x ")" << std::endl; #define DEBUG(x) std::cerr << #x << " = " << (x) << std::endl; void init_io() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); } int solve_case() { int n; std::cin >> n; std::vector<int> count(2*n + 1, 0); bool leftmost = true; int len = 0; REP(i, n) { char c; std::cin >> c; if (c=='0') { ++len; } else if (c=='1') { if (leftmost) { count[2 * len] += 1; } else { count[len] += 2; } len = 0; leftmost = false; } else { assert(false); } } if (leftmost) { // no virus return 0; } count[2*len] += 1; int saved = 0; int water = 0; FORD(len, 2*n, 1) { REP(i, count[len]) { if (len <= water) goto done; saved += len - water; water += 2; } } done: return n - (saved + 1) / 2; } int main() { init_io(); int ntc; std::cin >> ntc; REP(tc, ntc) { int res = solve_case(); std::cout << res << '\n'; } } |