#include <iostream> #include <cmath> #include <vector> #include <algorithm> using namespace std; int main() { ios::sync_with_stdio(false); int t; cin >> t; for (int test = 0; test < t; test++) { int numberOfCities; cin >> numberOfCities; string infectedCities; cin >> infectedCities; int i = 0; int leftIslandSize = 0, rightIslandSize = 0; while (i < infectedCities.size() && infectedCities[i] == '0') { leftIslandSize++; i++; } if (leftIslandSize == infectedCities.size()) { cout << 0 << endl; continue; } i = infectedCities.size() - 1; while (i >= 0 && infectedCities[i] == '0') { rightIslandSize++; i--; } vector<int> uninfectedIslands; i = leftIslandSize; int lastCityIndex = infectedCities.size() - 1 - rightIslandSize; while (i < lastCityIndex) { while (i < lastCityIndex && infectedCities[i] == '1') { i++; } int currentIslandSize = 0; while (i < lastCityIndex && infectedCities[i] == '0') { i++; currentIslandSize++; } if (currentIslandSize > 0) { uninfectedIslands.push_back(currentIslandSize); } } if (uninfectedIslands.size() == 0) { int result = leftIslandSize + rightIslandSize - 1; if (leftIslandSize == 0 || rightIslandSize == 0) { result++; } cout << numberOfCities - result << endl; continue; } sort(uninfectedIslands.begin(), uninfectedIslands.end(), greater<int>()); if (leftIslandSize < rightIslandSize) { int tmp = leftIslandSize; leftIslandSize = rightIslandSize; rightIslandSize = tmp; } int day = 0; int citiesSaved = 0; bool leftUsed = false, rightUsed = false; for (int i = 0; i < uninfectedIslands.size(); i++) { if (!leftUsed) { if (uninfectedIslands[i] < 2 * leftIslandSize && leftIslandSize - day > 0) { citiesSaved += leftIslandSize - day; day++; leftUsed = true; } } if (!rightUsed) { if (uninfectedIslands[i] < 2 * rightIslandSize && rightIslandSize - day > 0) { citiesSaved += rightIslandSize - day; day++; rightUsed = true; } } int currentSavedCities = uninfectedIslands[i] - 2 * day; if (currentSavedCities > 1) { currentSavedCities--; } if (currentSavedCities < 1) { break; } citiesSaved += currentSavedCities; day += 2; } if (!leftUsed && leftIslandSize - day > 0) { citiesSaved += leftIslandSize - day; day++; } if (!rightUsed && rightIslandSize - day > 0) { citiesSaved += rightIslandSize - day; } cout << numberOfCities - citiesSaved << endl; } }
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 | #include <iostream> #include <cmath> #include <vector> #include <algorithm> using namespace std; int main() { ios::sync_with_stdio(false); int t; cin >> t; for (int test = 0; test < t; test++) { int numberOfCities; cin >> numberOfCities; string infectedCities; cin >> infectedCities; int i = 0; int leftIslandSize = 0, rightIslandSize = 0; while (i < infectedCities.size() && infectedCities[i] == '0') { leftIslandSize++; i++; } if (leftIslandSize == infectedCities.size()) { cout << 0 << endl; continue; } i = infectedCities.size() - 1; while (i >= 0 && infectedCities[i] == '0') { rightIslandSize++; i--; } vector<int> uninfectedIslands; i = leftIslandSize; int lastCityIndex = infectedCities.size() - 1 - rightIslandSize; while (i < lastCityIndex) { while (i < lastCityIndex && infectedCities[i] == '1') { i++; } int currentIslandSize = 0; while (i < lastCityIndex && infectedCities[i] == '0') { i++; currentIslandSize++; } if (currentIslandSize > 0) { uninfectedIslands.push_back(currentIslandSize); } } if (uninfectedIslands.size() == 0) { int result = leftIslandSize + rightIslandSize - 1; if (leftIslandSize == 0 || rightIslandSize == 0) { result++; } cout << numberOfCities - result << endl; continue; } sort(uninfectedIslands.begin(), uninfectedIslands.end(), greater<int>()); if (leftIslandSize < rightIslandSize) { int tmp = leftIslandSize; leftIslandSize = rightIslandSize; rightIslandSize = tmp; } int day = 0; int citiesSaved = 0; bool leftUsed = false, rightUsed = false; for (int i = 0; i < uninfectedIslands.size(); i++) { if (!leftUsed) { if (uninfectedIslands[i] < 2 * leftIslandSize && leftIslandSize - day > 0) { citiesSaved += leftIslandSize - day; day++; leftUsed = true; } } if (!rightUsed) { if (uninfectedIslands[i] < 2 * rightIslandSize && rightIslandSize - day > 0) { citiesSaved += rightIslandSize - day; day++; rightUsed = true; } } int currentSavedCities = uninfectedIslands[i] - 2 * day; if (currentSavedCities > 1) { currentSavedCities--; } if (currentSavedCities < 1) { break; } citiesSaved += currentSavedCities; day += 2; } if (!leftUsed && leftIslandSize - day > 0) { citiesSaved += leftIslandSize - day; day++; } if (!rightUsed && rightIslandSize - day > 0) { citiesSaved += rightIslandSize - day; } cout << numberOfCities - citiesSaved << endl; } } |