#include <cstdio> #include <algorithm> #include <vector> #include <deque> using namespace std; typedef long long ll; const int INF = 2100000000; const int MAXN = 2048; int x[MAXN]; int y[MAXN]; int d[MAXN]; bool coversAll(int j, int n) { for (int i = 0; i < n; ++i) { if (x[i] < x[j] || y[i] < y[j]) return false; } return true; } bool intersect(int j, int i) { int ax = x[j]; int bx = x[j] + d[j]; int ay = y[j]; int by = y[j] + d[j]; int cx = x[i]; int dx = x[i] + d[i]; int cy = y[i]; int dy = y[i] + d[i]; return (ax < dx && cx < bx && ay < dy && cy < by); } bool overlapping(int n) { for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { if (intersect(i, j)) return true; } } return false; } int squareDistance(int i, int j) { if (x[j] <= x[i] && y[j] <= y[i]) return max(x[i] - x[j], y[i] - y[j]); if (x[j] <= x[i] && y[j] < y[i] + d[i]) return x[i] - x[j]; if (y[j] <= y[i] && x[j] < x[i] + d[i]) return y[i] - y[j]; return INF; } int findBoundry(int j, int n) { int mind = INF; for (int i = 0; i < n; ++i) { if (i != j) mind = min(mind, squareDistance(i, j)); } return mind; } bool limits(int j, int i) { return (squareDistance(i, j) != INF); } void createSquare(int j, int b) { d[j] = b; } void updateStairs(vector<int>& s, int j) { bool del[MAXN] = {false}; for (int i = 0; i < s.size(); ++i) { if (limits(j, s[i])) { createSquare(s[i], squareDistance(s[i], j)); del[i] = true; } } int tail = 0; for (int i = 0; i < s.size(); ++i) { if (!del[i]) s[tail++] = s[i]; } while (s.size() > tail) s.pop_back(); } void createSquare(int j, int b, vector<int>& s) { d[j] = b; updateStairs(s, j); } struct point { int x; int y; int id; point() {} point(int a, int b, int i) : x(a), y(b), id(i) {} }; bool operator<(const point& a, const point& b) { return (a.y == b.y ? a.x < b.x : a.y < b.y); } bool processStairs(vector<int>& s, int x1, int y1, int x2, int y2) { vector<point> st; for (int i = 0; i < s.size(); ++i) { st.push_back(point(x[s[i]], y[s[i]], s[i])); } if (st.size() == 0) return true; sort(st.begin(), st.end()); reverse(st.begin(), st.end()); vector<int> dx; vector<int> dy; if (st.front().x == x1) dy.push_back(INF); else dy.push_back(y2 - st.front().y); if (st.size() > 1) dx.push_back(st[1].x - st[0].x); else dx.push_back(INF); if (st.size() == 1) { d[st[0].id] = min(min(dx.front(), dy.front()), 1); return true; } for (int i = 1; i < st.size() - 1; ++i) { dx.push_back(st[i + 1].x - st[i].x); dy.push_back(st[i].y - st[i + 1].y); } if (st.back().x == x2) dx.push_back(INF); else dx.push_back(x2 - st.back().x); dy.push_back(st[st.size() - 2].y - st.back().y); if (dy.front() == INF && dx.back() == INF) { d[st.back().id] = dy.back(); d[st.front().id] = x2 - x1 + dy.back(); return true; } return false; } bool validateArea(int n, ll expectedArea) { ll area = 0; for (int i = 0; i < n; ++i) { area += (ll) d[i] * d[i]; } return area == expectedArea; } bool doit(int n) { if (!coversAll(0, n)) return false; vector<int> s; for (int i = 0; i < n; ++i) { int b = findBoundry(i, n); if (b == INF) s.push_back(i); else createSquare(i, b, s); } if (overlapping(n)) return false; int x1 = INF; int y1 = INF; int x2 = 0; int y2 = 0; for (int i = 0; i < n; ++i) { x1 = min(x1, x[i]); y1 = min(y1, y[i]); x2 = max(x2, x[i] + d[i]); y2 = max(y2, y[i] + d[i]); } if (!processStairs(s, x1, y1, x2, y2)) return false; for (int i = 0; i < n; ++i) { x2 = max(x2, x[i] + d[i]); y2 = max(y2, y[i] + d[i]); } ll area = (ll) (x2 - x1) * (y2 - y1); return validateArea(n, area); } int main() { point p[MAXN]; int t, n; scanf("%d", &t); while (t--) { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d%d", &p[i].x, &p[i].y); d[i] = 0; } sort(p, p + n); for (int i = 0; i < n; ++i) { x[i] = p[i].x; y[i] = p[i].y; } if (doit(n)) { printf("TAK"); for (int i = 0; i < n; ++i) printf(" %d", d[i]); printf("\n"); } else printf("NIE\n"); } }
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 <cstdio> #include <algorithm> #include <vector> #include <deque> using namespace std; typedef long long ll; const int INF = 2100000000; const int MAXN = 2048; int x[MAXN]; int y[MAXN]; int d[MAXN]; bool coversAll(int j, int n) { for (int i = 0; i < n; ++i) { if (x[i] < x[j] || y[i] < y[j]) return false; } return true; } bool intersect(int j, int i) { int ax = x[j]; int bx = x[j] + d[j]; int ay = y[j]; int by = y[j] + d[j]; int cx = x[i]; int dx = x[i] + d[i]; int cy = y[i]; int dy = y[i] + d[i]; return (ax < dx && cx < bx && ay < dy && cy < by); } bool overlapping(int n) { for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { if (intersect(i, j)) return true; } } return false; } int squareDistance(int i, int j) { if (x[j] <= x[i] && y[j] <= y[i]) return max(x[i] - x[j], y[i] - y[j]); if (x[j] <= x[i] && y[j] < y[i] + d[i]) return x[i] - x[j]; if (y[j] <= y[i] && x[j] < x[i] + d[i]) return y[i] - y[j]; return INF; } int findBoundry(int j, int n) { int mind = INF; for (int i = 0; i < n; ++i) { if (i != j) mind = min(mind, squareDistance(i, j)); } return mind; } bool limits(int j, int i) { return (squareDistance(i, j) != INF); } void createSquare(int j, int b) { d[j] = b; } void updateStairs(vector<int>& s, int j) { bool del[MAXN] = {false}; for (int i = 0; i < s.size(); ++i) { if (limits(j, s[i])) { createSquare(s[i], squareDistance(s[i], j)); del[i] = true; } } int tail = 0; for (int i = 0; i < s.size(); ++i) { if (!del[i]) s[tail++] = s[i]; } while (s.size() > tail) s.pop_back(); } void createSquare(int j, int b, vector<int>& s) { d[j] = b; updateStairs(s, j); } struct point { int x; int y; int id; point() {} point(int a, int b, int i) : x(a), y(b), id(i) {} }; bool operator<(const point& a, const point& b) { return (a.y == b.y ? a.x < b.x : a.y < b.y); } bool processStairs(vector<int>& s, int x1, int y1, int x2, int y2) { vector<point> st; for (int i = 0; i < s.size(); ++i) { st.push_back(point(x[s[i]], y[s[i]], s[i])); } if (st.size() == 0) return true; sort(st.begin(), st.end()); reverse(st.begin(), st.end()); vector<int> dx; vector<int> dy; if (st.front().x == x1) dy.push_back(INF); else dy.push_back(y2 - st.front().y); if (st.size() > 1) dx.push_back(st[1].x - st[0].x); else dx.push_back(INF); if (st.size() == 1) { d[st[0].id] = min(min(dx.front(), dy.front()), 1); return true; } for (int i = 1; i < st.size() - 1; ++i) { dx.push_back(st[i + 1].x - st[i].x); dy.push_back(st[i].y - st[i + 1].y); } if (st.back().x == x2) dx.push_back(INF); else dx.push_back(x2 - st.back().x); dy.push_back(st[st.size() - 2].y - st.back().y); if (dy.front() == INF && dx.back() == INF) { d[st.back().id] = dy.back(); d[st.front().id] = x2 - x1 + dy.back(); return true; } return false; } bool validateArea(int n, ll expectedArea) { ll area = 0; for (int i = 0; i < n; ++i) { area += (ll) d[i] * d[i]; } return area == expectedArea; } bool doit(int n) { if (!coversAll(0, n)) return false; vector<int> s; for (int i = 0; i < n; ++i) { int b = findBoundry(i, n); if (b == INF) s.push_back(i); else createSquare(i, b, s); } if (overlapping(n)) return false; int x1 = INF; int y1 = INF; int x2 = 0; int y2 = 0; for (int i = 0; i < n; ++i) { x1 = min(x1, x[i]); y1 = min(y1, y[i]); x2 = max(x2, x[i] + d[i]); y2 = max(y2, y[i] + d[i]); } if (!processStairs(s, x1, y1, x2, y2)) return false; for (int i = 0; i < n; ++i) { x2 = max(x2, x[i] + d[i]); y2 = max(y2, y[i] + d[i]); } ll area = (ll) (x2 - x1) * (y2 - y1); return validateArea(n, area); } int main() { point p[MAXN]; int t, n; scanf("%d", &t); while (t--) { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d%d", &p[i].x, &p[i].y); d[i] = 0; } sort(p, p + n); for (int i = 0; i < n; ++i) { x[i] = p[i].x; y[i] = p[i].y; } if (doit(n)) { printf("TAK"); for (int i = 0; i < n; ++i) printf(" %d", d[i]); printf("\n"); } else printf("NIE\n"); } } |