#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <queue> #include <algorithm> using namespace std; typedef long long i_t; inline i_t maxgap(i_t g, i_t generation) { i_t left = g - generation - generation - 1; i_t left1 = (g - generation) == 1 ? 1 : 0; if (left > 0) return left; if (left == 0) return 1; return 0; } void country() { int n; scanf("%i\n", &n); vector<i_t> gaps; i_t bgn_size = 0; i_t end_size = 0; i_t curr_size = 0; i_t special = 1; for (int i = 0; i < n; i++) { int ch = getchar(); if (ch == '1') { if (curr_size > 0) { if (special) bgn_size = curr_size; else gaps.push_back(curr_size); curr_size = 0; special = 0; } } else { ++curr_size; } } if (curr_size) end_size = curr_size; if (bgn_size < end_size) std::swap(bgn_size, end_size); std::sort(gaps.begin(), gaps.end(), std::greater<i_t>()); i_t generation = 0; i_t healthy = 0; if (gaps.empty()) { if (bgn_size == n) healthy = n; else { healthy = bgn_size + end_size - 1; } } for (i_t k = 0; k < (i_t)(gaps.size()); ++k) { auto g = gaps.at(k); if (maxgap(g, generation) <= bgn_size - generation) { i_t left = bgn_size - generation; generation += 1; if (left > 0) healthy += left; bgn_size = 0; } if (maxgap(g, generation) <= end_size - generation) { i_t left = end_size - generation; generation += 1; if (left > 0) healthy += left; end_size = 0; } { i_t left = g - generation - generation - 1; generation += 2; if (left > 0) healthy += left; else if (left == 0) { --generation; healthy += 1; }; } } printf("%lld\n", n-healthy); } int main() { int n; scanf("%i\n", &n); for (size_t i = 0; i < n; i++) { country(); } 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 | #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <queue> #include <algorithm> using namespace std; typedef long long i_t; inline i_t maxgap(i_t g, i_t generation) { i_t left = g - generation - generation - 1; i_t left1 = (g - generation) == 1 ? 1 : 0; if (left > 0) return left; if (left == 0) return 1; return 0; } void country() { int n; scanf("%i\n", &n); vector<i_t> gaps; i_t bgn_size = 0; i_t end_size = 0; i_t curr_size = 0; i_t special = 1; for (int i = 0; i < n; i++) { int ch = getchar(); if (ch == '1') { if (curr_size > 0) { if (special) bgn_size = curr_size; else gaps.push_back(curr_size); curr_size = 0; special = 0; } } else { ++curr_size; } } if (curr_size) end_size = curr_size; if (bgn_size < end_size) std::swap(bgn_size, end_size); std::sort(gaps.begin(), gaps.end(), std::greater<i_t>()); i_t generation = 0; i_t healthy = 0; if (gaps.empty()) { if (bgn_size == n) healthy = n; else { healthy = bgn_size + end_size - 1; } } for (i_t k = 0; k < (i_t)(gaps.size()); ++k) { auto g = gaps.at(k); if (maxgap(g, generation) <= bgn_size - generation) { i_t left = bgn_size - generation; generation += 1; if (left > 0) healthy += left; bgn_size = 0; } if (maxgap(g, generation) <= end_size - generation) { i_t left = end_size - generation; generation += 1; if (left > 0) healthy += left; end_size = 0; } { i_t left = g - generation - generation - 1; generation += 2; if (left > 0) healthy += left; else if (left == 0) { --generation; healthy += 1; }; } } printf("%lld\n", n-healthy); } int main() { int n; scanf("%i\n", &n); for (size_t i = 0; i < n; i++) { country(); } return 0; } |