#include <cstdio> #include <list> #include <math.h> using namespace std; struct CitySequence { int infected; int length; int from_left; int from_right; bool processed; CitySequence(int length, int infected) { this->length = length; this->infected = infected; this->from_left = 1; this->from_right = 1; this->processed = false; } void print(int day) { printf( "day: %d, processed: %d, infected: %d, length: %d, from_left: %d, from_right: %d, cities: %d, saves: %d\n", day, this->processed ? 1 : 0, this->infected, this->length, this->from_left, this->from_right, this->get_cities_left(day), this->get_possible_saves(day) ); } int get_cities_left(int day) const { return max(this->length - (this->from_left + this->from_right) * day, 0); } int get_possible_saves(int day) const { int cities_left = this->get_cities_left(day); if (cities_left) { if (this->from_left + this->from_right == 2 && cities_left < 3) { return 0; } return cities_left - (this->from_left + this->from_right) - ((this->from_left + this->from_right)/2); } return 0; } ~CitySequence() {} }; int get_sequence_length(char A[], int start, int end) { char c = A[start]; int result = 0; while(start + result < end && A[start + result] == c) result++; return result; } int get_solution(list<CitySequence>& city_sequences, int n) { int cities_saved = 0; int day = 0; CitySequence* first_sequence = &city_sequences.front(); CitySequence* last_sequence = &city_sequences.back(); list<CitySequence>::iterator it; city_sequences.sort([](const CitySequence& a, const CitySequence& b) { int a_saves = a.get_possible_saves(0); int b_saves = b.get_possible_saves(0); return a_saves > b_saves; }); for (it = city_sequences.begin(); it != city_sequences.end();) { CitySequence* sequence_to_process = &(*it); int first_possible_saves = first_sequence->get_possible_saves(day); int last_possible_saves = last_sequence->get_possible_saves(day); int vaccines = 0; bool swapped = false; bool weird = false; if (!first_sequence->processed && first_possible_saves >= sequence_to_process->get_possible_saves(day)) { sequence_to_process = first_sequence; swapped = true; } if (!last_sequence->processed && last_possible_saves >= sequence_to_process->get_possible_saves(day)) { sequence_to_process = last_sequence; swapped = true; } int possible_saves = sequence_to_process->get_possible_saves(day); int cities_left = sequence_to_process->get_cities_left(day); if (!sequence_to_process->processed && possible_saves) { // sequence_to_process->print(day); cities_saved += possible_saves; vaccines = cities_left > 2 ? sequence_to_process->from_left + sequence_to_process->from_right : min(1, sequence_to_process->from_left + sequence_to_process->from_right); cities_saved += vaccines; day += vaccines; sequence_to_process->processed = true; if (swapped) weird = true; } if (!weird) { ++it; } } for (auto &city_sequence : city_sequences) { int cities_left = city_sequence.get_cities_left(day); int vaccines = 0; if (!city_sequence.processed && cities_left) { // city_sequence.print(day); vaccines = cities_left > 2 ? city_sequence.from_left + city_sequence.from_right : min(1, city_sequence.from_left + city_sequence.from_right); cities_saved += vaccines; day += vaccines; } } return n - cities_saved; } int main(int argc, char const *argv[]) { int t; scanf("%d", &t); for (size_t i = 0; i < t; i++) { int n; list<CitySequence> city_sequences; char* cities; scanf("%d", &n); // printf("%d\n", n); cities = new char[n + 100]; scanf("%s", cities); for (int j = 0; j < n;) { char infected = cities[j] - '0'; int length = get_sequence_length(cities, j, n); CitySequence new_sequence = CitySequence(length, infected); city_sequences.push_back(new_sequence); j += length; } city_sequences.front().from_left = 0; city_sequences.back().from_right = 0; city_sequences.remove_if( [](const CitySequence& city) { return city.infected; } ); // for (auto &&city_sequece : city_sequences) { // city_sequece.print(0); // } printf("%d\n", get_solution(city_sequences, n)); delete[] cities; } 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | #include <cstdio> #include <list> #include <math.h> using namespace std; struct CitySequence { int infected; int length; int from_left; int from_right; bool processed; CitySequence(int length, int infected) { this->length = length; this->infected = infected; this->from_left = 1; this->from_right = 1; this->processed = false; } void print(int day) { printf( "day: %d, processed: %d, infected: %d, length: %d, from_left: %d, from_right: %d, cities: %d, saves: %d\n", day, this->processed ? 1 : 0, this->infected, this->length, this->from_left, this->from_right, this->get_cities_left(day), this->get_possible_saves(day) ); } int get_cities_left(int day) const { return max(this->length - (this->from_left + this->from_right) * day, 0); } int get_possible_saves(int day) const { int cities_left = this->get_cities_left(day); if (cities_left) { if (this->from_left + this->from_right == 2 && cities_left < 3) { return 0; } return cities_left - (this->from_left + this->from_right) - ((this->from_left + this->from_right)/2); } return 0; } ~CitySequence() {} }; int get_sequence_length(char A[], int start, int end) { char c = A[start]; int result = 0; while(start + result < end && A[start + result] == c) result++; return result; } int get_solution(list<CitySequence>& city_sequences, int n) { int cities_saved = 0; int day = 0; CitySequence* first_sequence = &city_sequences.front(); CitySequence* last_sequence = &city_sequences.back(); list<CitySequence>::iterator it; city_sequences.sort([](const CitySequence& a, const CitySequence& b) { int a_saves = a.get_possible_saves(0); int b_saves = b.get_possible_saves(0); return a_saves > b_saves; }); for (it = city_sequences.begin(); it != city_sequences.end();) { CitySequence* sequence_to_process = &(*it); int first_possible_saves = first_sequence->get_possible_saves(day); int last_possible_saves = last_sequence->get_possible_saves(day); int vaccines = 0; bool swapped = false; bool weird = false; if (!first_sequence->processed && first_possible_saves >= sequence_to_process->get_possible_saves(day)) { sequence_to_process = first_sequence; swapped = true; } if (!last_sequence->processed && last_possible_saves >= sequence_to_process->get_possible_saves(day)) { sequence_to_process = last_sequence; swapped = true; } int possible_saves = sequence_to_process->get_possible_saves(day); int cities_left = sequence_to_process->get_cities_left(day); if (!sequence_to_process->processed && possible_saves) { // sequence_to_process->print(day); cities_saved += possible_saves; vaccines = cities_left > 2 ? sequence_to_process->from_left + sequence_to_process->from_right : min(1, sequence_to_process->from_left + sequence_to_process->from_right); cities_saved += vaccines; day += vaccines; sequence_to_process->processed = true; if (swapped) weird = true; } if (!weird) { ++it; } } for (auto &city_sequence : city_sequences) { int cities_left = city_sequence.get_cities_left(day); int vaccines = 0; if (!city_sequence.processed && cities_left) { // city_sequence.print(day); vaccines = cities_left > 2 ? city_sequence.from_left + city_sequence.from_right : min(1, city_sequence.from_left + city_sequence.from_right); cities_saved += vaccines; day += vaccines; } } return n - cities_saved; } int main(int argc, char const *argv[]) { int t; scanf("%d", &t); for (size_t i = 0; i < t; i++) { int n; list<CitySequence> city_sequences; char* cities; scanf("%d", &n); // printf("%d\n", n); cities = new char[n + 100]; scanf("%s", cities); for (int j = 0; j < n;) { char infected = cities[j] - '0'; int length = get_sequence_length(cities, j, n); CitySequence new_sequence = CitySequence(length, infected); city_sequences.push_back(new_sequence); j += length; } city_sequences.front().from_left = 0; city_sequences.back().from_right = 0; city_sequences.remove_if( [](const CitySequence& city) { return city.infected; } ); // for (auto &&city_sequece : city_sequences) { // city_sequece.print(0); // } printf("%d\n", get_solution(city_sequences, n)); delete[] cities; } return 0; } |