#include <iostream> #include <vector> #include <algorithm> struct Area { char prev{'b'}; char next{'e'}; int a{0}; int b{0}; bool used{false}; void print() { std::cout << "p:" << prev << " n:" << next << " " << a << " " << b << '\n'; } }; bool operator>(const Area& l, const Area& r) { int free_cities_l = l.b - l.a; int free_cities_r = r.b - r.a; return free_cities_l > free_cities_r; } int solution(std::vector<Area>& areas, std::vector<Area> borders, int infected) { if (areas.empty()) { if(infected > 0) { if (borders.size() <= 1) { return infected; } return infected + 1; } return 0; } std::sort(areas.begin(), areas.end(), std::greater<Area>{}); std::sort(borders.begin(), borders.end(), std::greater<Area>{}); // std::cout << "areas: \n"; // for(auto& a :areas) { // a.print(); // } // std::cout << "\nborders: \n"; // for(auto& b : borders) { // b.print(); // } int day = 0; auto it = areas.begin(); bool border_used = false; while(it != areas.end()) { // for(auto& area : areas) { auto& area = *it; border_used = false; // std::cout << loop++ << ":\n"; int a = area.a + day; int b = area.b - day; int c_left = b - a; if(c_left <= 0) { // std::cout << "wa" << area.a << area.b << '\n'; infected += area.b - area.a; ++it; continue; } for(auto& border : borders) { if(border.used) { continue; } int ba = border.a; if(border.prev != 'b') { ba += day; } int bb = border.b; if(border.next != 'e') { bb -= day; } int b_c_left = bb - ba; if(b_c_left <= 0) { continue; } //border is more important to save if(b_c_left + 2 >= c_left) { // std::cout << border.prev << " " << border.next << '\n'; // std::cout << "border over area\n"; infected += day; // std::cout << day << " " << infected << '\n'; border.used = true; border_used = true; ++day; break; } } if(border_used) { continue; } //left is right only one vac this day for both if(a == b-1) { infected += 2*day; // std::cout << "one vac " << day << " " << infected << '\n'; ++day; ++it; continue; } //vac left size, count left infected += day; // std::cout << "left " << day << " " << infected << '\n'; ++day; --b; //count right infected += day; //right is left, no need to vac if(b-1 <= a) { ++it; continue; } //right was vaccinated ++day; ++it; } for (auto& border : borders) { if (border.used) { continue; } int ba = border.a; if (border.prev != 'b') { ba += day; } int bb = border.b; if (border.next != 'e') { bb -= day; } int b_c_left = bb - ba; if (b_c_left <= 0) { infected += border.b - border.a; continue; } infected += day; ++day; } return infected; } int main() { int t; int n; std::cin >> t; int tc = 1; while(t--) { std::cin >> n; int count = 0; char buff; char prev = 'b'; Area area; int i=0; std::vector<Area> areas; std::vector<Area> borders; while(n--) { std::cin >> buff; if(buff == '1') { //close current area if(prev == '0') { area.b = i; area.next = '1'; if(area.prev == 'b') { borders.push_back(area); } else { areas.push_back(area); } area.next = 'e'; } ++count; } else { //open new area if(prev == '1' || prev == 'b') { area.prev = prev; area.a = i; } } prev = buff; ++i; } //...111000 //check if last area was closed or is open if(prev == '0') { area.b = i; borders.push_back(area); } std::cout << solution(areas, borders, count) << '\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 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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | #include <iostream> #include <vector> #include <algorithm> struct Area { char prev{'b'}; char next{'e'}; int a{0}; int b{0}; bool used{false}; void print() { std::cout << "p:" << prev << " n:" << next << " " << a << " " << b << '\n'; } }; bool operator>(const Area& l, const Area& r) { int free_cities_l = l.b - l.a; int free_cities_r = r.b - r.a; return free_cities_l > free_cities_r; } int solution(std::vector<Area>& areas, std::vector<Area> borders, int infected) { if (areas.empty()) { if(infected > 0) { if (borders.size() <= 1) { return infected; } return infected + 1; } return 0; } std::sort(areas.begin(), areas.end(), std::greater<Area>{}); std::sort(borders.begin(), borders.end(), std::greater<Area>{}); // std::cout << "areas: \n"; // for(auto& a :areas) { // a.print(); // } // std::cout << "\nborders: \n"; // for(auto& b : borders) { // b.print(); // } int day = 0; auto it = areas.begin(); bool border_used = false; while(it != areas.end()) { // for(auto& area : areas) { auto& area = *it; border_used = false; // std::cout << loop++ << ":\n"; int a = area.a + day; int b = area.b - day; int c_left = b - a; if(c_left <= 0) { // std::cout << "wa" << area.a << area.b << '\n'; infected += area.b - area.a; ++it; continue; } for(auto& border : borders) { if(border.used) { continue; } int ba = border.a; if(border.prev != 'b') { ba += day; } int bb = border.b; if(border.next != 'e') { bb -= day; } int b_c_left = bb - ba; if(b_c_left <= 0) { continue; } //border is more important to save if(b_c_left + 2 >= c_left) { // std::cout << border.prev << " " << border.next << '\n'; // std::cout << "border over area\n"; infected += day; // std::cout << day << " " << infected << '\n'; border.used = true; border_used = true; ++day; break; } } if(border_used) { continue; } //left is right only one vac this day for both if(a == b-1) { infected += 2*day; // std::cout << "one vac " << day << " " << infected << '\n'; ++day; ++it; continue; } //vac left size, count left infected += day; // std::cout << "left " << day << " " << infected << '\n'; ++day; --b; //count right infected += day; //right is left, no need to vac if(b-1 <= a) { ++it; continue; } //right was vaccinated ++day; ++it; } for (auto& border : borders) { if (border.used) { continue; } int ba = border.a; if (border.prev != 'b') { ba += day; } int bb = border.b; if (border.next != 'e') { bb -= day; } int b_c_left = bb - ba; if (b_c_left <= 0) { infected += border.b - border.a; continue; } infected += day; ++day; } return infected; } int main() { int t; int n; std::cin >> t; int tc = 1; while(t--) { std::cin >> n; int count = 0; char buff; char prev = 'b'; Area area; int i=0; std::vector<Area> areas; std::vector<Area> borders; while(n--) { std::cin >> buff; if(buff == '1') { //close current area if(prev == '0') { area.b = i; area.next = '1'; if(area.prev == 'b') { borders.push_back(area); } else { areas.push_back(area); } area.next = 'e'; } ++count; } else { //open new area if(prev == '1' || prev == 'b') { area.prev = prev; area.a = i; } } prev = buff; ++i; } //...111000 //check if last area was closed or is open if(prev == '0') { area.b = i; borders.push_back(area); } std::cout << solution(areas, borders, count) << '\n'; } return 0; } |