#include <iostream> #include <array> constexpr int MAX_N = 100000; void peekUntillTowns() { char c = std::cin.peek(); while (c != '1' && c != '0') { getchar(); c = std::cin.peek(); } } int towns[MAX_N]; int n = 0; int firstStreak = 0; int lastStreak = 0; bool areAnyTownsSick = false; int highestStreak = 0; bool areThereCitiesToRescue(const int& streakLength, const int& turnsPassed) { return streakLength - 2 * turnsPassed > 0; } bool shouldRescueOneWayStreak(const int& oneWayStreakCurrentLength, const int& currentLength) { return oneWayStreakCurrentLength > currentLength / 2; } bool shouldRescueFirstStreak(const int& firstStreakCurrentLength, const int& lastStreakCurrentLength, const int& currentLength) { return firstStreakCurrentLength > currentLength / 2 && firstStreakCurrentLength >= lastStreakCurrentLength; } bool shouldRescueLastStreak(const int& firstStreakCurrentLength, const int& lastStreakCurrentLength, const int& currentLength) { return lastStreakCurrentLength > currentLength / 2 && lastStreakCurrentLength >= firstStreakCurrentLength; } int solveOne() { // reset variables for (int i = highestStreak; i >= 0; i--) towns[i] = 0; n = 0; firstStreak = 0; lastStreak = 0; areAnyTownsSick = false; highestStreak = 0; // start parsing std::cin >> n; peekUntillTowns(); int currentTownStreak = 0; bool isFirstStreakRead = false; for (int i = 0; i < n; i++) { char c = getchar(); if (c == '0') { currentTownStreak++; if (i == n - 1) { lastStreak = currentTownStreak; } } else { areAnyTownsSick = true; if (!isFirstStreakRead) { firstStreak = currentTownStreak; isFirstStreakRead = true; } else { towns[currentTownStreak]++; if (highestStreak < currentTownStreak) highestStreak = currentTownStreak; } currentTownStreak = 0; } } // everything parsed // no sick towns case if (!areAnyTownsSick) { return 0; } int rescuedTowns = 0; int turnsPassed = 0; int i = highestStreak; while (i > 0) { if (!areThereCitiesToRescue(i, turnsPassed)) break; int townStreaksWithLengthI = towns[i]; if (townStreaksWithLengthI <= 0) { i--; continue; } int currentLength = i - 2 * turnsPassed; if (shouldRescueFirstStreak(firstStreak - turnsPassed, lastStreak - turnsPassed, currentLength)) { rescuedTowns += firstStreak - turnsPassed; firstStreak = 0; turnsPassed += 1; continue; } if (shouldRescueLastStreak(firstStreak - turnsPassed, lastStreak - turnsPassed, currentLength)) { rescuedTowns += lastStreak - turnsPassed; lastStreak = 0; turnsPassed += 1; continue; } if (currentLength <= 2) { rescuedTowns += 1; turnsPassed += 1; } else { rescuedTowns += currentLength - 1; turnsPassed += 2; } towns[i]--; } // bidirectional calculated if (firstStreak - turnsPassed > 0) { rescuedTowns += firstStreak - turnsPassed; turnsPassed += 1; } if (lastStreak - turnsPassed > 0) { rescuedTowns += lastStreak - turnsPassed; turnsPassed += 1; } return n - rescuedTowns; } void solveAll() { int t = 0;; int result = 0; std::cin >> t; for (int i = 0; i < t; i++) { std::cout << solveOne(); if (i < t - 1) std::cout << std::endl; } } int main() { solveAll(); }
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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | #include <iostream> #include <array> constexpr int MAX_N = 100000; void peekUntillTowns() { char c = std::cin.peek(); while (c != '1' && c != '0') { getchar(); c = std::cin.peek(); } } int towns[MAX_N]; int n = 0; int firstStreak = 0; int lastStreak = 0; bool areAnyTownsSick = false; int highestStreak = 0; bool areThereCitiesToRescue(const int& streakLength, const int& turnsPassed) { return streakLength - 2 * turnsPassed > 0; } bool shouldRescueOneWayStreak(const int& oneWayStreakCurrentLength, const int& currentLength) { return oneWayStreakCurrentLength > currentLength / 2; } bool shouldRescueFirstStreak(const int& firstStreakCurrentLength, const int& lastStreakCurrentLength, const int& currentLength) { return firstStreakCurrentLength > currentLength / 2 && firstStreakCurrentLength >= lastStreakCurrentLength; } bool shouldRescueLastStreak(const int& firstStreakCurrentLength, const int& lastStreakCurrentLength, const int& currentLength) { return lastStreakCurrentLength > currentLength / 2 && lastStreakCurrentLength >= firstStreakCurrentLength; } int solveOne() { // reset variables for (int i = highestStreak; i >= 0; i--) towns[i] = 0; n = 0; firstStreak = 0; lastStreak = 0; areAnyTownsSick = false; highestStreak = 0; // start parsing std::cin >> n; peekUntillTowns(); int currentTownStreak = 0; bool isFirstStreakRead = false; for (int i = 0; i < n; i++) { char c = getchar(); if (c == '0') { currentTownStreak++; if (i == n - 1) { lastStreak = currentTownStreak; } } else { areAnyTownsSick = true; if (!isFirstStreakRead) { firstStreak = currentTownStreak; isFirstStreakRead = true; } else { towns[currentTownStreak]++; if (highestStreak < currentTownStreak) highestStreak = currentTownStreak; } currentTownStreak = 0; } } // everything parsed // no sick towns case if (!areAnyTownsSick) { return 0; } int rescuedTowns = 0; int turnsPassed = 0; int i = highestStreak; while (i > 0) { if (!areThereCitiesToRescue(i, turnsPassed)) break; int townStreaksWithLengthI = towns[i]; if (townStreaksWithLengthI <= 0) { i--; continue; } int currentLength = i - 2 * turnsPassed; if (shouldRescueFirstStreak(firstStreak - turnsPassed, lastStreak - turnsPassed, currentLength)) { rescuedTowns += firstStreak - turnsPassed; firstStreak = 0; turnsPassed += 1; continue; } if (shouldRescueLastStreak(firstStreak - turnsPassed, lastStreak - turnsPassed, currentLength)) { rescuedTowns += lastStreak - turnsPassed; lastStreak = 0; turnsPassed += 1; continue; } if (currentLength <= 2) { rescuedTowns += 1; turnsPassed += 1; } else { rescuedTowns += currentLength - 1; turnsPassed += 2; } towns[i]--; } // bidirectional calculated if (firstStreak - turnsPassed > 0) { rescuedTowns += firstStreak - turnsPassed; turnsPassed += 1; } if (lastStreak - turnsPassed > 0) { rescuedTowns += lastStreak - turnsPassed; turnsPassed += 1; } return n - rescuedTowns; } void solveAll() { int t = 0;; int result = 0; std::cin >> t; for (int i = 0; i < t; i++) { std::cout << solveOne(); if (i < t - 1) std::cout << std::endl; } } int main() { solveAll(); } |