#include <cstdio> #include <vector> #include <utility> #include <algorithm> constexpr int N = 100*1000+10; char cities[N]; int main() { int t; scanf("%d", &t); while (t > 0) { int n; scanf("%d %s", &n, cities); std::vector<std::pair<int, int>> healthy; int i = 0; int current = 0; while (i < n && cities[i] == '0') { i++; current++; } if (current > 0) { healthy.emplace_back(2 * current, current); } i++; current = 0; while (i < n) { while (i < n && cities[i] == '0') { i++; current++; } if (i < n) { if (current > 0) { healthy.emplace_back(current, current); } current = 0; i++; } } if (current > 0) { healthy.emplace_back(2 * current, current); } std::sort(healthy.begin(), healthy.end(), std::greater<std::pair<int, int>>()); int result = 0; int turn = 0; for (i = 0; i < healthy.size(); ++i) { if (healthy[i].first <= turn * 2) { break; } if (healthy[i].first == healthy[i].second) { if (healthy[i].second == 2 * turn + 1) { result++; turn++; } else { result += healthy[i].second - (2 * turn + 1); turn += 2; } } else { result += healthy[i].second - turn; turn++; } } printf("%d\n", n - result); t--; } }
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 | #include <cstdio> #include <vector> #include <utility> #include <algorithm> constexpr int N = 100*1000+10; char cities[N]; int main() { int t; scanf("%d", &t); while (t > 0) { int n; scanf("%d %s", &n, cities); std::vector<std::pair<int, int>> healthy; int i = 0; int current = 0; while (i < n && cities[i] == '0') { i++; current++; } if (current > 0) { healthy.emplace_back(2 * current, current); } i++; current = 0; while (i < n) { while (i < n && cities[i] == '0') { i++; current++; } if (i < n) { if (current > 0) { healthy.emplace_back(current, current); } current = 0; i++; } } if (current > 0) { healthy.emplace_back(2 * current, current); } std::sort(healthy.begin(), healthy.end(), std::greater<std::pair<int, int>>()); int result = 0; int turn = 0; for (i = 0; i < healthy.size(); ++i) { if (healthy[i].first <= turn * 2) { break; } if (healthy[i].first == healthy[i].second) { if (healthy[i].second == 2 * turn + 1) { result++; turn++; } else { result += healthy[i].second - (2 * turn + 1); turn += 2; } } else { result += healthy[i].second - turn; turn++; } } printf("%d\n", n - result); t--; } } |