#include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge {c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #undef assert #define assert struct P { int x, y; bool operator == (const P & he) const { return x == he.x && y == he.y; } }; debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ") "; return dd; } vector<P> read() { int n; scanf("%d", &n); vector<int> t(n); for(int & x : t) scanf("%d", &x); vector<P> they; for(int i = 0; i < n; i += 2) { they.push_back(P{t[i], t[i+1]}); they.push_back(P{t[(i+2)%n], t[i+1]}); } assert(they[0].y == they[1].y); return they; } vector<pair<pair<P, P>,int>> segments; long double z; int g_answer = 0; void maxInside(const vector<P> & they, bool special, int my_dist, int low_hack) { if(special) --my_dist; const int n = they.size(); vector<pair<P, P>> consider; for(int i = 0; i < n; ++i) { P a = they[i], b = they[(i+1)%n]; if(a.x < b.x && !special) consider.push_back({a, b}); if(a.x > b.x && special) consider.push_back({b, a}); } random_shuffle(consider.begin(), consider.end()); for(const pair<P, P> & line : consider) { //~ bool flag = line == make_pair(P{4, 17}, P{5, 17}); //~ if(flag) debug() << imie(line); auto check = [&] (const int answer) { debug() << "?" imie(answer); //~ if(flag) debug() << "check" imie(line) imie(answer) imie(my_dist); assert(line.first.x < line.second.x); const int x1 = line.first.x - (answer - my_dist); const int x2 = line.second.x + (answer - my_dist) - 1; const int y = line.first.y + (answer - my_dist); assert(x1 < x2); int first_good = x1; for(const pair<pair<P,P>, int> & segment : segments) { //~ if(flag) debug() << imie(segment); const P & a = segment.first.first; const P & b = segment.first.second; int his_dist = segment.second; if(his_dist >= low_hack) --his_dist; if(b.y + (answer - his_dist) - 1 < y) continue; if(a.y - (answer - his_dist) > y) continue; int low_x = a.x - (answer - his_dist); int high_x = b.x + (answer - his_dist) - 1; //~ if(flag) debug() << imie(low_x) imie(high_x); if(first_good < low_x) { debug() << ">>>>>>>>>>>>>>>" imie(first_good) imie(low_x) imie(segment) imie(line); //~ assert(false); // remove return true; } first_good = max(first_good, high_x + 1); if(first_good > x2) return false; //~ if(segment.second.y > y0 && segment.first.y <= y0 + side) { //~ if(segment.second.y <= y0) continue; //~ if(segment.first.y > y0 + side) continue; //~ if(flag) debug() << imie(prev_bad) imie(x) imie(segment); //~ if(min(segment.first.x - side, x2) > prev_bad) return true; //~ prev_bad = max(prev_bad, segment.second.x); } return false; }; if(check(g_answer + 1)) { //~ puts("x"); ++g_answer; int k = 0; while(check(g_answer + (1 << k))) { g_answer += 1 << k; ++k; } while(k >= 0) { if(check(g_answer + (1 << k))) g_answer += 1 << k; --k; } } } } bool contains(const vector<P> & a, const vector<P> & b) { P p = b[0]; int cnt = 0; for(int i = 0; i < (int) a.size(); i += 2) { int j = i + 1; assert(a[i].y == a[j].y); if(a[i].y <= p.y) continue; if((a[i].x <= p.x) != (a[j].x <= p.x)) ++cnt; } return cnt % 2; } //~ int FastMax(int x, int y) { return x - y >> 31 & (x ^ y) ^ x; } //~ int FastMin(int x, int y) { return x - y >> 31 & (x ^ y) ^ y; } //~ int FastAbs(int x) { return (x ^ x >> 31) - (x >> 31); } int distance(const vector<P> & a, const vector<P> & b) { //~ return 0; //~ bool flag = rand() % 10 == 0; //~ if(flag)z -= clock(); int r = INT_MAX; for(const P & one : a) for(const P & two : b) r = min(r, max(abs(one.x - two.x), abs(one.y - two.y))); for(int turn : {0, 1}) { const vector<P> & A = turn ? a : b; const vector<P> & B = turn ? b : a; for(const P & p : A) { /*if((B[0].x <= p.x) != (B[1].x <= p.x) && abs(p.y - B[0].y) < r) r = abs(p.y - B[0].y); if((B[1].y <= p.y) != (B[2].y <= p.y) && abs(p.x - B[1].x) < r) r = abs(p.x - B[1].x); if((B[2].x <= p.x) != (B[3].x <= p.x) && abs(p.y - B[2].y) < r) r = abs(p.y - B[2].y); if((B[3].y <= p.y) != (B[0].y <= p.y) && abs(p.x - B[3].x) < r) r = abs(p.x - B[3].x);*/ for(int i = 0; i < (int) B.size() - 2; i += 2) { if((B[i].x <= p.x) != (B[i+1].x <= p.x) && abs(p.y - B[i].y) < r) r = abs(p.y - B[i].y); //~ r = min(r, abs(p.y - B[i].y)); if((B[i+1].y <= p.y) != (B[i+2].y <= p.y) && abs(p.x - B[i+1].x) < r) r = abs(p.x - B[i+1].x); //~ r = min(r, abs(p.x - B[i+1].x)); } const int i = (int) B.size() - 2; if((B[i].x <= p.x) != (B[i+1].x <= p.x) && abs(p.y - B[i].y) < r) r = abs(p.y - B[i].y); if((B[i+1].y <= p.y) != (B[0].y <= p.y) && abs(p.x - B[i+1].x) < r) r = abs(p.x - B[i+1].x); } } //~ if(flag) z += clock(); return r - 1; } int main() { int T; scanf("%d", &T); vector<vector<P>> all(T); for(int i = 0; i < T; ++i) all[i] = read(); int x0 = INT_MAX; for(P p : all[0]) x0 = min(x0, p.x); for(int i = 1; i < T; ++i) { int here = INT_MAX; for(P p : all[i]) here = min(here, p.x); if(here < x0) { x0 = here; swap(all[0], all[i]); } } vector<int> dist(T, INT_MAX); vector<bool> done(T, false); vector<int> parent(T, -1); dist[0] = 1; for(int rep = 0; rep < T; ++rep) { int who = -1; for(int i = 0; i < T; ++i) if(!done[i]) if(who == -1 || dist[i] < dist[who]) who = i; assert(who != -1); assert(!done[who]); done[who] = true; for(int other = 0; other < T; ++other) if(!done[other]) { bool inside = contains(all[who], all[other]); if(inside) parent[other] = who; int maybe = dist[who] + distance(all[who], all[other]) + int(inside); if(maybe < dist[other]) { dist[other] = maybe; } // dist[other] = min(dist[other], dist[who] + distance) } } for(int who = 0; who < (int) all.size(); ++who) { const vector<P> & they = all[who]; const int n = they.size(); for(int i = 0; i < n; ++i) { P a = they[i], b = they[(i+1)%n]; if(make_pair(a.x, a.y) > make_pair(b.x, b.y)) swap(a, b); segments.push_back(make_pair(make_pair(a, b), dist[who])); } } sort(segments.begin(), segments.end(), [&] (const pair<pair<P,P>, int> & a, const pair<pair<P,P>, int> & b) { if(a.first.first.x + a.second != b.first.first.x + b.second) return a.first.first.x + a.second < b.first.first.x + b.second; return a.second > b.second; }); for(int i = 0; i < T; ++i) g_answer = max(g_answer, dist[i]); vector<int> indices(T); for(int i = 0; i < T; ++i) indices[i] = i; random_shuffle(indices.begin(), indices.end()); for(int who : indices) { //~ int low_hack = my_dist; //~ if(!special) ++low_hack; if(parent[who] != -1) maxInside(all[who], true, dist[who], dist[parent[who]] + 1); maxInside(all[who], false, dist[who], dist[who] + 1); } //~ vector<bool> isParent(T, false); //~ for(int i = 0; i < T; ++i) //~ if(parent[i] != -1) //~ isParent[parent[i]] = true; //~ int answer = 0; //~ for(int i = 0; i < T; ++i) //~ if(!isParent[i]) //~ answer = max(answer, dist[i] + maxInside(all[i])); assert(g_answer); printf("%d\n", g_answer); //~ printf("%.10Lf\n", 10 * z / CLOCKS_PER_SEC); // pierwszy to otoczka //~ for(int i = 0; i < T; ++i) //~ printf("%d\n", maxInside(all[i])); }
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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | #include <bits/stdc++.h> using namespace std; #define sim template < class c #define ris return * this #define dor > debug & operator << #define eni(x) sim > typename \ enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) { sim > struct rge {c b, e; }; sim > rge<c> range(c i, c j) { return rge<c>{i, j}; } sim > auto dud(c* x) -> decltype(cerr << *x, 0); sim > char dud(...); struct debug { #ifdef LOCAL ~debug() { cerr << endl; } eni(!=) cerr << boolalpha << i; ris; } eni(==) ris << range(begin(i), end(i)); } sim, class b dor(pair < b, c > d) { ris << "(" << d.first << ", " << d.second << ")"; } sim dor(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) *this << ", " + 2 * (it == d.b) << *it; ris << "]"; } #else sim dor(const c&) { ris; } #endif }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #undef assert #define assert struct P { int x, y; bool operator == (const P & he) const { return x == he.x && y == he.y; } }; debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ") "; return dd; } vector<P> read() { int n; scanf("%d", &n); vector<int> t(n); for(int & x : t) scanf("%d", &x); vector<P> they; for(int i = 0; i < n; i += 2) { they.push_back(P{t[i], t[i+1]}); they.push_back(P{t[(i+2)%n], t[i+1]}); } assert(they[0].y == they[1].y); return they; } vector<pair<pair<P, P>,int>> segments; long double z; int g_answer = 0; void maxInside(const vector<P> & they, bool special, int my_dist, int low_hack) { if(special) --my_dist; const int n = they.size(); vector<pair<P, P>> consider; for(int i = 0; i < n; ++i) { P a = they[i], b = they[(i+1)%n]; if(a.x < b.x && !special) consider.push_back({a, b}); if(a.x > b.x && special) consider.push_back({b, a}); } random_shuffle(consider.begin(), consider.end()); for(const pair<P, P> & line : consider) { //~ bool flag = line == make_pair(P{4, 17}, P{5, 17}); //~ if(flag) debug() << imie(line); auto check = [&] (const int answer) { debug() << "?" imie(answer); //~ if(flag) debug() << "check" imie(line) imie(answer) imie(my_dist); assert(line.first.x < line.second.x); const int x1 = line.first.x - (answer - my_dist); const int x2 = line.second.x + (answer - my_dist) - 1; const int y = line.first.y + (answer - my_dist); assert(x1 < x2); int first_good = x1; for(const pair<pair<P,P>, int> & segment : segments) { //~ if(flag) debug() << imie(segment); const P & a = segment.first.first; const P & b = segment.first.second; int his_dist = segment.second; if(his_dist >= low_hack) --his_dist; if(b.y + (answer - his_dist) - 1 < y) continue; if(a.y - (answer - his_dist) > y) continue; int low_x = a.x - (answer - his_dist); int high_x = b.x + (answer - his_dist) - 1; //~ if(flag) debug() << imie(low_x) imie(high_x); if(first_good < low_x) { debug() << ">>>>>>>>>>>>>>>" imie(first_good) imie(low_x) imie(segment) imie(line); //~ assert(false); // remove return true; } first_good = max(first_good, high_x + 1); if(first_good > x2) return false; //~ if(segment.second.y > y0 && segment.first.y <= y0 + side) { //~ if(segment.second.y <= y0) continue; //~ if(segment.first.y > y0 + side) continue; //~ if(flag) debug() << imie(prev_bad) imie(x) imie(segment); //~ if(min(segment.first.x - side, x2) > prev_bad) return true; //~ prev_bad = max(prev_bad, segment.second.x); } return false; }; if(check(g_answer + 1)) { //~ puts("x"); ++g_answer; int k = 0; while(check(g_answer + (1 << k))) { g_answer += 1 << k; ++k; } while(k >= 0) { if(check(g_answer + (1 << k))) g_answer += 1 << k; --k; } } } } bool contains(const vector<P> & a, const vector<P> & b) { P p = b[0]; int cnt = 0; for(int i = 0; i < (int) a.size(); i += 2) { int j = i + 1; assert(a[i].y == a[j].y); if(a[i].y <= p.y) continue; if((a[i].x <= p.x) != (a[j].x <= p.x)) ++cnt; } return cnt % 2; } //~ int FastMax(int x, int y) { return x - y >> 31 & (x ^ y) ^ x; } //~ int FastMin(int x, int y) { return x - y >> 31 & (x ^ y) ^ y; } //~ int FastAbs(int x) { return (x ^ x >> 31) - (x >> 31); } int distance(const vector<P> & a, const vector<P> & b) { //~ return 0; //~ bool flag = rand() % 10 == 0; //~ if(flag)z -= clock(); int r = INT_MAX; for(const P & one : a) for(const P & two : b) r = min(r, max(abs(one.x - two.x), abs(one.y - two.y))); for(int turn : {0, 1}) { const vector<P> & A = turn ? a : b; const vector<P> & B = turn ? b : a; for(const P & p : A) { /*if((B[0].x <= p.x) != (B[1].x <= p.x) && abs(p.y - B[0].y) < r) r = abs(p.y - B[0].y); if((B[1].y <= p.y) != (B[2].y <= p.y) && abs(p.x - B[1].x) < r) r = abs(p.x - B[1].x); if((B[2].x <= p.x) != (B[3].x <= p.x) && abs(p.y - B[2].y) < r) r = abs(p.y - B[2].y); if((B[3].y <= p.y) != (B[0].y <= p.y) && abs(p.x - B[3].x) < r) r = abs(p.x - B[3].x);*/ for(int i = 0; i < (int) B.size() - 2; i += 2) { if((B[i].x <= p.x) != (B[i+1].x <= p.x) && abs(p.y - B[i].y) < r) r = abs(p.y - B[i].y); //~ r = min(r, abs(p.y - B[i].y)); if((B[i+1].y <= p.y) != (B[i+2].y <= p.y) && abs(p.x - B[i+1].x) < r) r = abs(p.x - B[i+1].x); //~ r = min(r, abs(p.x - B[i+1].x)); } const int i = (int) B.size() - 2; if((B[i].x <= p.x) != (B[i+1].x <= p.x) && abs(p.y - B[i].y) < r) r = abs(p.y - B[i].y); if((B[i+1].y <= p.y) != (B[0].y <= p.y) && abs(p.x - B[i+1].x) < r) r = abs(p.x - B[i+1].x); } } //~ if(flag) z += clock(); return r - 1; } int main() { int T; scanf("%d", &T); vector<vector<P>> all(T); for(int i = 0; i < T; ++i) all[i] = read(); int x0 = INT_MAX; for(P p : all[0]) x0 = min(x0, p.x); for(int i = 1; i < T; ++i) { int here = INT_MAX; for(P p : all[i]) here = min(here, p.x); if(here < x0) { x0 = here; swap(all[0], all[i]); } } vector<int> dist(T, INT_MAX); vector<bool> done(T, false); vector<int> parent(T, -1); dist[0] = 1; for(int rep = 0; rep < T; ++rep) { int who = -1; for(int i = 0; i < T; ++i) if(!done[i]) if(who == -1 || dist[i] < dist[who]) who = i; assert(who != -1); assert(!done[who]); done[who] = true; for(int other = 0; other < T; ++other) if(!done[other]) { bool inside = contains(all[who], all[other]); if(inside) parent[other] = who; int maybe = dist[who] + distance(all[who], all[other]) + int(inside); if(maybe < dist[other]) { dist[other] = maybe; } // dist[other] = min(dist[other], dist[who] + distance) } } for(int who = 0; who < (int) all.size(); ++who) { const vector<P> & they = all[who]; const int n = they.size(); for(int i = 0; i < n; ++i) { P a = they[i], b = they[(i+1)%n]; if(make_pair(a.x, a.y) > make_pair(b.x, b.y)) swap(a, b); segments.push_back(make_pair(make_pair(a, b), dist[who])); } } sort(segments.begin(), segments.end(), [&] (const pair<pair<P,P>, int> & a, const pair<pair<P,P>, int> & b) { if(a.first.first.x + a.second != b.first.first.x + b.second) return a.first.first.x + a.second < b.first.first.x + b.second; return a.second > b.second; }); for(int i = 0; i < T; ++i) g_answer = max(g_answer, dist[i]); vector<int> indices(T); for(int i = 0; i < T; ++i) indices[i] = i; random_shuffle(indices.begin(), indices.end()); for(int who : indices) { //~ int low_hack = my_dist; //~ if(!special) ++low_hack; if(parent[who] != -1) maxInside(all[who], true, dist[who], dist[parent[who]] + 1); maxInside(all[who], false, dist[who], dist[who] + 1); } //~ vector<bool> isParent(T, false); //~ for(int i = 0; i < T; ++i) //~ if(parent[i] != -1) //~ isParent[parent[i]] = true; //~ int answer = 0; //~ for(int i = 0; i < T; ++i) //~ if(!isParent[i]) //~ answer = max(answer, dist[i] + maxInside(all[i])); assert(g_answer); printf("%d\n", g_answer); //~ printf("%.10Lf\n", 10 * z / CLOCKS_PER_SEC); // pierwszy to otoczka //~ for(int i = 0; i < T; ++i) //~ printf("%d\n", maxInside(all[i])); } |