#include <cstdlib> #include <cmath> #include <cstdio> #include <algorithm> #include <vector> #include <set> using namespace std; using lint = long long int; using ulint = unsigned long long int; #define span(x) x.begin(), x.end() #define rev_span(x) x.rbegin(), x.rend() const lint Inf = 2*1000000000+3; const lint MaxN = 2000+3; struct point { lint x, y; point() {} }; point P[MaxN]; lint UpConstr[MaxN]; lint RightConstr[MaxN]; lint Result[MaxN]; struct cmpX { bool operator () (lint i1, lint i2) const { return P[i1].x < P[i2].x; } }; void solveCase(); int main() { int t; scanf("%d", &t); for(int i = 0; i < t; ++i) solveCase(); return 0; } void solveCase() { lint n; scanf("%lld", &n); for(lint i = 0; i < n; ++i) scanf("%lld%lld", &P[i].x, &P[i].y); if(n == 1) { puts("TAK 1"); return; } lint min_x = Inf, min_y = Inf; for(lint i = 0; i < n; ++i) min_x = min(min_x, P[i].x), min_y = min(min_y, P[i].y); bool rootExists = false; for(lint i = 0; i < n; ++i) { if(P[i].x == min_x && P[i].y == min_y) rootExists = true; } if(!rootExists) { puts("NIE"); return; } fill_n(Result, n, Inf); fill_n(UpConstr, n, 0); fill_n(RightConstr, n, 0); ulint area = 0ull; for(lint i = 0; i < n; ++i) { for(lint j = 0; j < n; ++j) { if(i == j) continue; lint dx = P[j].x - P[i].x; lint dy = P[j].y - P[i].y; if(dx >= 0 && dy >= 0) { lint max_d = max(dx, dy); Result[i] = min(Result[i], max_d); } } } set<lint, cmpX> notSolved; for(lint i = 0; i < n; ++i) if(Result[i] != Inf) area += (ulint)Result[i] * (ulint)Result[i]; else notSolved.insert(i); lint max_x = 0; lint max_y = 0; if(notSolved.size() == 1) { lint curr = *notSolved.begin(); if(P[0].y == P[1].y) { for(lint i = 0; i < n; ++i) if(Result[i] != Inf) max_y = max(max_y, P[0].y + Result[i]); Result[curr] = max_y-min_y; area += (ulint)Result[curr] * (ulint)Result[curr]; max_x = P[curr].x + Result[curr]; } else { for(lint i = 0; i < n; ++i) if(Result[i] != Inf) max_x = max(max_x, P[0].x + Result[i]); Result[curr] = max_x-min_x; area += (ulint)Result[curr] * (ulint)Result[curr]; max_y = P[curr].y + Result[curr]; } } else { bool shouldProcess = true; while(shouldProcess) { shouldProcess = false; vector<lint> v(span(notSolved)); for(lint i = 1; i < v.size()-1; ++i) { lint prev = v[i-1]; lint curr = v[i]; lint next = v[i+1]; lint dy = P[prev].y - P[curr].y; lint dx = P[next].x - P[curr].x; if(dy > dx) RightConstr[next] = dy - dx; else if(dx > dy) UpConstr[prev] = dx - dy; } if(max_y == 0 && UpConstr[v.front()] > 0) { shouldProcess = true; lint curr = v[0]; lint next = v[1]; lint dx = P[next].x - P[curr].x; if(dx != UpConstr[curr]) { puts("NIE"); return; } else { shouldProcess = true; Result[curr] = dx; area += (ulint)dx * (ulint)dx; max_y = P[curr].y + dx; P[curr].y = max_y; } } if(max_x == 0 && RightConstr[v.back()] > 0) { shouldProcess = true; lint curr = v[v.size()-1]; lint prev = v[v.size()-2]; lint dy = P[prev].y - P[curr].y; if(dy != RightConstr[curr]) { puts("NIE"); return; } else { shouldProcess = true; Result[curr] = dy; area += (ulint)dy * (ulint)dy; max_x = P[curr].x + dy; P[curr].x = max_x; } } for(lint i = 1; i < v.size()-1; ++i) { lint prev = v[i-1]; lint curr = v[i]; lint next = v[i+1]; lint dy = P[prev].y - P[curr].y; lint dx = P[next].x - P[curr].x; if(RightConstr[curr] > 0) { if(dy < dx) { puts("NIE"); return; } else { shouldProcess = true; Result[curr] = dy; area += (ulint)dy * (ulint)dy; notSolved.erase(curr); } } if(UpConstr[curr] > 0) { if(dx < dy) { puts("NIE"); return; } else if(Result[curr] == Inf) { shouldProcess = true; Result[curr] = dx; area += (ulint)dx * (ulint)dx; notSolved.erase(curr); } } } if(!shouldProcess && v.size() > 2) { lint prev = v[0]; lint curr = v[1]; lint next = v[2]; lint dy = P[prev].y - P[curr].y; lint dx = P[next].x - P[curr].x; lint max_d = max(dx, dy); shouldProcess = true; Result[curr] = max_d; area += (ulint)max_d * (ulint)max_d; notSolved.erase(curr); } } if(max_x == 0) { lint leftMost = *notSolved.begin(); lint curr = *notSolved.rbegin(); lint dy = max(max_y-min_y, P[leftMost].y) - P[curr].y; Result[curr] = dy; area += (ulint)dy * (ulint)dy; max_x = P[curr].x + dy; } if(max_y == 0) { lint curr = *notSolved.begin(); lint dx = (max_x-min_x); Result[curr] = dx; area += (ulint)dx * (ulint)dx; max_y = P[curr].y + dx; } } if(area == (ulint)(max_x-min_x) * (ulint)(max_y-min_y)) { printf("TAK"); for(lint i = 0; i < n; ++i) printf(" %lld", Result[i]); puts(""); } else { puts("NIE"); } }
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 | #include <cstdlib> #include <cmath> #include <cstdio> #include <algorithm> #include <vector> #include <set> using namespace std; using lint = long long int; using ulint = unsigned long long int; #define span(x) x.begin(), x.end() #define rev_span(x) x.rbegin(), x.rend() const lint Inf = 2*1000000000+3; const lint MaxN = 2000+3; struct point { lint x, y; point() {} }; point P[MaxN]; lint UpConstr[MaxN]; lint RightConstr[MaxN]; lint Result[MaxN]; struct cmpX { bool operator () (lint i1, lint i2) const { return P[i1].x < P[i2].x; } }; void solveCase(); int main() { int t; scanf("%d", &t); for(int i = 0; i < t; ++i) solveCase(); return 0; } void solveCase() { lint n; scanf("%lld", &n); for(lint i = 0; i < n; ++i) scanf("%lld%lld", &P[i].x, &P[i].y); if(n == 1) { puts("TAK 1"); return; } lint min_x = Inf, min_y = Inf; for(lint i = 0; i < n; ++i) min_x = min(min_x, P[i].x), min_y = min(min_y, P[i].y); bool rootExists = false; for(lint i = 0; i < n; ++i) { if(P[i].x == min_x && P[i].y == min_y) rootExists = true; } if(!rootExists) { puts("NIE"); return; } fill_n(Result, n, Inf); fill_n(UpConstr, n, 0); fill_n(RightConstr, n, 0); ulint area = 0ull; for(lint i = 0; i < n; ++i) { for(lint j = 0; j < n; ++j) { if(i == j) continue; lint dx = P[j].x - P[i].x; lint dy = P[j].y - P[i].y; if(dx >= 0 && dy >= 0) { lint max_d = max(dx, dy); Result[i] = min(Result[i], max_d); } } } set<lint, cmpX> notSolved; for(lint i = 0; i < n; ++i) if(Result[i] != Inf) area += (ulint)Result[i] * (ulint)Result[i]; else notSolved.insert(i); lint max_x = 0; lint max_y = 0; if(notSolved.size() == 1) { lint curr = *notSolved.begin(); if(P[0].y == P[1].y) { for(lint i = 0; i < n; ++i) if(Result[i] != Inf) max_y = max(max_y, P[0].y + Result[i]); Result[curr] = max_y-min_y; area += (ulint)Result[curr] * (ulint)Result[curr]; max_x = P[curr].x + Result[curr]; } else { for(lint i = 0; i < n; ++i) if(Result[i] != Inf) max_x = max(max_x, P[0].x + Result[i]); Result[curr] = max_x-min_x; area += (ulint)Result[curr] * (ulint)Result[curr]; max_y = P[curr].y + Result[curr]; } } else { bool shouldProcess = true; while(shouldProcess) { shouldProcess = false; vector<lint> v(span(notSolved)); for(lint i = 1; i < v.size()-1; ++i) { lint prev = v[i-1]; lint curr = v[i]; lint next = v[i+1]; lint dy = P[prev].y - P[curr].y; lint dx = P[next].x - P[curr].x; if(dy > dx) RightConstr[next] = dy - dx; else if(dx > dy) UpConstr[prev] = dx - dy; } if(max_y == 0 && UpConstr[v.front()] > 0) { shouldProcess = true; lint curr = v[0]; lint next = v[1]; lint dx = P[next].x - P[curr].x; if(dx != UpConstr[curr]) { puts("NIE"); return; } else { shouldProcess = true; Result[curr] = dx; area += (ulint)dx * (ulint)dx; max_y = P[curr].y + dx; P[curr].y = max_y; } } if(max_x == 0 && RightConstr[v.back()] > 0) { shouldProcess = true; lint curr = v[v.size()-1]; lint prev = v[v.size()-2]; lint dy = P[prev].y - P[curr].y; if(dy != RightConstr[curr]) { puts("NIE"); return; } else { shouldProcess = true; Result[curr] = dy; area += (ulint)dy * (ulint)dy; max_x = P[curr].x + dy; P[curr].x = max_x; } } for(lint i = 1; i < v.size()-1; ++i) { lint prev = v[i-1]; lint curr = v[i]; lint next = v[i+1]; lint dy = P[prev].y - P[curr].y; lint dx = P[next].x - P[curr].x; if(RightConstr[curr] > 0) { if(dy < dx) { puts("NIE"); return; } else { shouldProcess = true; Result[curr] = dy; area += (ulint)dy * (ulint)dy; notSolved.erase(curr); } } if(UpConstr[curr] > 0) { if(dx < dy) { puts("NIE"); return; } else if(Result[curr] == Inf) { shouldProcess = true; Result[curr] = dx; area += (ulint)dx * (ulint)dx; notSolved.erase(curr); } } } if(!shouldProcess && v.size() > 2) { lint prev = v[0]; lint curr = v[1]; lint next = v[2]; lint dy = P[prev].y - P[curr].y; lint dx = P[next].x - P[curr].x; lint max_d = max(dx, dy); shouldProcess = true; Result[curr] = max_d; area += (ulint)max_d * (ulint)max_d; notSolved.erase(curr); } } if(max_x == 0) { lint leftMost = *notSolved.begin(); lint curr = *notSolved.rbegin(); lint dy = max(max_y-min_y, P[leftMost].y) - P[curr].y; Result[curr] = dy; area += (ulint)dy * (ulint)dy; max_x = P[curr].x + dy; } if(max_y == 0) { lint curr = *notSolved.begin(); lint dx = (max_x-min_x); Result[curr] = dx; area += (ulint)dx * (ulint)dx; max_y = P[curr].y + dx; } } if(area == (ulint)(max_x-min_x) * (ulint)(max_y-min_y)) { printf("TAK"); for(lint i = 0; i < n; ++i) printf(" %lld", Result[i]); puts(""); } else { puts("NIE"); } } |