#include <cstdio> #include <queue> void przypadek() { int n; scanf("%d\n", &n); std::priority_queue<int> one; std::priority_queue<int> two; int i = 0; int c = 0; char x; for (; i < n; i++) { scanf("%c", &x); if (x == '0') c++; else { if (c > 0) one.push(c); c = 0; for (i++; i < n; i++) { scanf("%c", &x); if (x == '0') { c++; break; } } break; } } for (i++; i < n; i++) { scanf("%c", &x); if (x == '0') c++; else { two.push(c); c = 0; for (i++; i < n; i++) { scanf("%c", &x); if (x == '0') { c++; break; } } } } if (c > 0) one.push(c); int p, q; int uratowane = 0; for (i = 0; !one.empty() || !two.empty(); i++) { if (!one.empty()) { p = one.top() - i; if (p < 1) one = {}; } else p = 0; if (!two.empty()) { q = two.top() - 2*i; if (q < 1) two = {}; } else q = 0; if (p+1 >= q && p > 0) { uratowane += p; one.pop(); } if (q > p+1 && q > 0 || (p == 0 && q == 1)) { uratowane += 1; one.push(q + i - 1); two.pop(); } } printf("%d\n", n - uratowane); } int main() { int t; scanf("%d\n", &t); for (int i = 0; i < t; i++) przypadek(); 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 | #include <cstdio> #include <queue> void przypadek() { int n; scanf("%d\n", &n); std::priority_queue<int> one; std::priority_queue<int> two; int i = 0; int c = 0; char x; for (; i < n; i++) { scanf("%c", &x); if (x == '0') c++; else { if (c > 0) one.push(c); c = 0; for (i++; i < n; i++) { scanf("%c", &x); if (x == '0') { c++; break; } } break; } } for (i++; i < n; i++) { scanf("%c", &x); if (x == '0') c++; else { two.push(c); c = 0; for (i++; i < n; i++) { scanf("%c", &x); if (x == '0') { c++; break; } } } } if (c > 0) one.push(c); int p, q; int uratowane = 0; for (i = 0; !one.empty() || !two.empty(); i++) { if (!one.empty()) { p = one.top() - i; if (p < 1) one = {}; } else p = 0; if (!two.empty()) { q = two.top() - 2*i; if (q < 1) two = {}; } else q = 0; if (p+1 >= q && p > 0) { uratowane += p; one.pop(); } if (q > p+1 && q > 0 || (p == 0 && q == 1)) { uratowane += 1; one.push(q + i - 1); two.pop(); } } printf("%d\n", n - uratowane); } int main() { int t; scanf("%d\n", &t); for (int i = 0; i < t; i++) przypadek(); return 0; } |