#include <iostream> #include <string> #include <vector> std::vector<std::pair<int, int>> getHealthyCities(std::string cities) { // (number of cities, how many times to vaccinate) std::vector<std::pair<int, int>> healthyCities; int numOfCities = 0; bool marginCity = true; for (int i = 0; i < cities.length(); i++) { if (cities[i] == '0') { numOfCities++; } else if (numOfCities != 0) { healthyCities.push_back(std::make_pair(numOfCities, numOfCities > 1 ? 2 : 1)); numOfCities = 0; } } if (numOfCities != 0) { healthyCities.push_back(std::make_pair(numOfCities, 1)); } if (cities[0] == '0') { healthyCities[0].second = 1; } return healthyCities; } int getNumOfInfectedCities(std::string cities) { int res = 0; for (int i = 0; i < cities.length(); i++) { if (cities[i] == '1') { res++; } } return res; } void vaccination(std::vector<std::pair<int, int>> &cities) { int max = 0; int index = 0; for (int i = 0; i < cities.size(); i++) { if (max < cities[i].first) { max = cities[i].first; index = i; } } if (max < 5) { if (cities.back().first >= 2 && cities.back().second == 1) { index = cities.size() - 1; } else if (cities.front().first >= 2 && cities.front().second == 1) { index = 0; } } if (max == 5) { if (cities.front().first + 1 == max && cities.front().second == 1) { index = 0; } else if (cities.back().first + 1 == max && cities.back().second == 1) { index = cities.size() - 1; } } cities[index].second--; if (cities[index].second == 0 || cities[index].first <= 1) { cities.erase(cities.begin()+ index); } } int infect(std::vector<std::pair<int, int>> &cities) { int res = 0; for (int i = 0; i < cities.size(); i++) { int toInfect = cities[i].first < cities[i].second ? 1 : cities[i].second; cities[i].first -= toInfect; res += toInfect; if (cities[i].first <= 0) { cities.erase(cities.begin() + i); i--; } } return res; } int main() { int t; // Number of test cases int* n; // number of cities std::string* city; std::cin >> t; n = new int[t]; city = new std::string[t]; for (int i = 0; i < t; i++) { std::cin >> n[i] >> city[i]; } for (int i = 0; i < t; i++) { int infected = getNumOfInfectedCities(city[i]); std::vector<std::pair<int, int>> healthCities = getHealthyCities(city[i]); while (healthCities.size() > 0) { vaccination(healthCities); infected += infect(healthCities); } // display std::cout << infected << "\n"; } 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 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 | #include <iostream> #include <string> #include <vector> std::vector<std::pair<int, int>> getHealthyCities(std::string cities) { // (number of cities, how many times to vaccinate) std::vector<std::pair<int, int>> healthyCities; int numOfCities = 0; bool marginCity = true; for (int i = 0; i < cities.length(); i++) { if (cities[i] == '0') { numOfCities++; } else if (numOfCities != 0) { healthyCities.push_back(std::make_pair(numOfCities, numOfCities > 1 ? 2 : 1)); numOfCities = 0; } } if (numOfCities != 0) { healthyCities.push_back(std::make_pair(numOfCities, 1)); } if (cities[0] == '0') { healthyCities[0].second = 1; } return healthyCities; } int getNumOfInfectedCities(std::string cities) { int res = 0; for (int i = 0; i < cities.length(); i++) { if (cities[i] == '1') { res++; } } return res; } void vaccination(std::vector<std::pair<int, int>> &cities) { int max = 0; int index = 0; for (int i = 0; i < cities.size(); i++) { if (max < cities[i].first) { max = cities[i].first; index = i; } } if (max < 5) { if (cities.back().first >= 2 && cities.back().second == 1) { index = cities.size() - 1; } else if (cities.front().first >= 2 && cities.front().second == 1) { index = 0; } } if (max == 5) { if (cities.front().first + 1 == max && cities.front().second == 1) { index = 0; } else if (cities.back().first + 1 == max && cities.back().second == 1) { index = cities.size() - 1; } } cities[index].second--; if (cities[index].second == 0 || cities[index].first <= 1) { cities.erase(cities.begin()+ index); } } int infect(std::vector<std::pair<int, int>> &cities) { int res = 0; for (int i = 0; i < cities.size(); i++) { int toInfect = cities[i].first < cities[i].second ? 1 : cities[i].second; cities[i].first -= toInfect; res += toInfect; if (cities[i].first <= 0) { cities.erase(cities.begin() + i); i--; } } return res; } int main() { int t; // Number of test cases int* n; // number of cities std::string* city; std::cin >> t; n = new int[t]; city = new std::string[t]; for (int i = 0; i < t; i++) { std::cin >> n[i] >> city[i]; } for (int i = 0; i < t; i++) { int infected = getNumOfInfectedCities(city[i]); std::vector<std::pair<int, int>> healthCities = getHealthyCities(city[i]); while (healthCities.size() > 0) { vaccination(healthCities); infected += infect(healthCities); } // display std::cout << infected << "\n"; } return 0; } |