#include <cstdio> #include <algorithm> #include <vector> #include <stack> #define REP(i, n) for (int i = 0; i < (n); ++i) #define FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define FORD(i, a, b) for (int i = (a); i >= (b); --i) #define PII pair<int, int> #define MP make_pair #define FI first #define SE second using namespace std; struct Point { Point() {} Point(int x, int y) : x(x), y(y) {} int x, y; }; const int maxn = 2010; const int inf = 2*1000*1000*1000 + 10; int t; int n; Point p[maxn]; Point maxp, minp; Point origmaxp, origminp; int s[maxn]; int dist(Point& a, Point& b) { return max(abs(a.x - b.x), abs(a.y - b.y)); } int maxSize(int index) { int res = inf; REP(i, n) { if (i != index && p[index].x <= p[i].x && p[index].y <= p[i].y) { res = min(res, dist(p[index], p[i])); } } return res; } vector<PII> v; int vs; stack<PII> st; int area; int sum; bool check() { area = (maxp.x - minp.x)*(maxp.y - minp.y); sum = 0; REP(i, n) { if (s[i] == inf) { return false; } sum += s[i]*s[i]; } return area == sum; } bool solve() { maxp = Point(-1, -1); minp = Point(inf, inf); v.clear(); REP(i, n) { s[i] = maxSize(i); minp.x = min(minp.x, p[i].x); minp.y = min(minp.y, p[i].y); if (s[i] < inf) { maxp.x = max(maxp.x, p[i].x + s[i]); maxp.y = max(maxp.y, p[i].y + s[i]); } else { v.push_back(MP(p[i].x, i)); // printf("v %d %d\n", p[i].x, p[i].y); } } // printf("TAK"); // REP(i, n) { // printf(" %d", s[i] == inf ? 0 : s[i]); // } // printf("\n"); origmaxp = maxp; origminp = minp; sort(v.begin(), v.end()); vs = v.size(); if (vs == 1) { if (maxp.x > p[v[vs - 1].SE].x && maxp.y > p[v[0].SE].y && maxp.x - p[v[vs - 1].SE].x != maxp.y - p[v[0].SE].y) { return false; } else if (maxp.y > p[v[0].SE].y) { s[v[0].SE] = maxp.y - p[v[0].SE].y; maxp.x = max(maxp.x, p[v[0].SE].x + s[v[0].SE]); maxp.y = max(maxp.y, p[v[0].SE].y + s[v[0].SE]); return check(); } else if (maxp.x > p[v[vs - 1].SE].x) { s[v[0].SE] = maxp.x - p[v[0].SE].x; maxp.x = max(maxp.x, p[v[0].SE].x + s[v[0].SE]); maxp.y = max(maxp.y, p[v[0].SE].y + s[v[0].SE]); return check(); } else { s[v[0].SE] = 1; return check(); } } // From bottom-right. maxp = origmaxp; minp = origminp; while (!st.empty()) { st.pop(); } FORD(i, vs - 1, 0) { while (!st.empty() && st.top().SE <= p[v[i].SE].y) { st.pop(); } if (i == vs - 1) { if (maxp.x > p[v[vs - 1].SE].x) { s[v[vs - 1].SE] = maxp.x - p[v[vs - 1].SE].x; } else { s[v[vs - 1].SE] = abs(p[v[vs - 1].SE].x - p[v[0].SE].x) + abs(p[v[vs - 1].SE].y - p[v[0].SE].y); } } else { // printf("n %d i %d vi %d %d %d\n", n, i, v[i].SE, v[vs - 1].SE, v[0].SE); s[v[i].SE] = (st.empty() ? maxp.x : st.top().FI) - p[v[i].SE].x; } maxp.x = max(maxp.x, p[v[i].SE].x + s[v[i].SE]); maxp.y = max(maxp.y, p[v[i].SE].y + s[v[i].SE]); st.push(MP(p[v[i].SE].x, p[v[i].SE].y + s[v[i].SE])); } if (check()) { return true; } // From top-left. maxp = origmaxp; minp = origminp; while (!st.empty()) { st.pop(); } REP(i, vs) { while (!st.empty() && st.top().SE <= p[v[i].SE].x) { st.pop(); } if (i == 0) { if (maxp.y > p[v[0].SE].y) { s[v[0].SE] = maxp.y - p[v[0].SE].y; } else { s[v[0].SE] = abs(p[v[0].SE].x - p[v[vs - 1].SE].x) + abs(p[v[0].SE].y - p[v[vs - 1].SE].y); } } else { s[v[i].SE] = (st.empty() ? maxp.y : st.top().FI) - p[v[i].SE].y; } maxp.x = max(maxp.x, p[v[i].SE].x + s[v[i].SE]); maxp.y = max(maxp.y, p[v[i].SE].y + s[v[i].SE]); st.push(MP(p[v[i].SE].y, p[v[i].SE].x + s[v[i].SE])); } if (check()) { return true; } return false; } int main() { scanf("%d", &t); REP(tt, t) { scanf("%d", &n); REP(i, n) { scanf("%d%d", &p[i].x, &p[i].y); } bool ok = solve(); if (ok) { printf("TAK"); REP(i, n) { printf(" %d", s[i]); } printf("\n"); } else { printf("NIE\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 | #include <cstdio> #include <algorithm> #include <vector> #include <stack> #define REP(i, n) for (int i = 0; i < (n); ++i) #define FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define FORD(i, a, b) for (int i = (a); i >= (b); --i) #define PII pair<int, int> #define MP make_pair #define FI first #define SE second using namespace std; struct Point { Point() {} Point(int x, int y) : x(x), y(y) {} int x, y; }; const int maxn = 2010; const int inf = 2*1000*1000*1000 + 10; int t; int n; Point p[maxn]; Point maxp, minp; Point origmaxp, origminp; int s[maxn]; int dist(Point& a, Point& b) { return max(abs(a.x - b.x), abs(a.y - b.y)); } int maxSize(int index) { int res = inf; REP(i, n) { if (i != index && p[index].x <= p[i].x && p[index].y <= p[i].y) { res = min(res, dist(p[index], p[i])); } } return res; } vector<PII> v; int vs; stack<PII> st; int area; int sum; bool check() { area = (maxp.x - minp.x)*(maxp.y - minp.y); sum = 0; REP(i, n) { if (s[i] == inf) { return false; } sum += s[i]*s[i]; } return area == sum; } bool solve() { maxp = Point(-1, -1); minp = Point(inf, inf); v.clear(); REP(i, n) { s[i] = maxSize(i); minp.x = min(minp.x, p[i].x); minp.y = min(minp.y, p[i].y); if (s[i] < inf) { maxp.x = max(maxp.x, p[i].x + s[i]); maxp.y = max(maxp.y, p[i].y + s[i]); } else { v.push_back(MP(p[i].x, i)); // printf("v %d %d\n", p[i].x, p[i].y); } } // printf("TAK"); // REP(i, n) { // printf(" %d", s[i] == inf ? 0 : s[i]); // } // printf("\n"); origmaxp = maxp; origminp = minp; sort(v.begin(), v.end()); vs = v.size(); if (vs == 1) { if (maxp.x > p[v[vs - 1].SE].x && maxp.y > p[v[0].SE].y && maxp.x - p[v[vs - 1].SE].x != maxp.y - p[v[0].SE].y) { return false; } else if (maxp.y > p[v[0].SE].y) { s[v[0].SE] = maxp.y - p[v[0].SE].y; maxp.x = max(maxp.x, p[v[0].SE].x + s[v[0].SE]); maxp.y = max(maxp.y, p[v[0].SE].y + s[v[0].SE]); return check(); } else if (maxp.x > p[v[vs - 1].SE].x) { s[v[0].SE] = maxp.x - p[v[0].SE].x; maxp.x = max(maxp.x, p[v[0].SE].x + s[v[0].SE]); maxp.y = max(maxp.y, p[v[0].SE].y + s[v[0].SE]); return check(); } else { s[v[0].SE] = 1; return check(); } } // From bottom-right. maxp = origmaxp; minp = origminp; while (!st.empty()) { st.pop(); } FORD(i, vs - 1, 0) { while (!st.empty() && st.top().SE <= p[v[i].SE].y) { st.pop(); } if (i == vs - 1) { if (maxp.x > p[v[vs - 1].SE].x) { s[v[vs - 1].SE] = maxp.x - p[v[vs - 1].SE].x; } else { s[v[vs - 1].SE] = abs(p[v[vs - 1].SE].x - p[v[0].SE].x) + abs(p[v[vs - 1].SE].y - p[v[0].SE].y); } } else { // printf("n %d i %d vi %d %d %d\n", n, i, v[i].SE, v[vs - 1].SE, v[0].SE); s[v[i].SE] = (st.empty() ? maxp.x : st.top().FI) - p[v[i].SE].x; } maxp.x = max(maxp.x, p[v[i].SE].x + s[v[i].SE]); maxp.y = max(maxp.y, p[v[i].SE].y + s[v[i].SE]); st.push(MP(p[v[i].SE].x, p[v[i].SE].y + s[v[i].SE])); } if (check()) { return true; } // From top-left. maxp = origmaxp; minp = origminp; while (!st.empty()) { st.pop(); } REP(i, vs) { while (!st.empty() && st.top().SE <= p[v[i].SE].x) { st.pop(); } if (i == 0) { if (maxp.y > p[v[0].SE].y) { s[v[0].SE] = maxp.y - p[v[0].SE].y; } else { s[v[0].SE] = abs(p[v[0].SE].x - p[v[vs - 1].SE].x) + abs(p[v[0].SE].y - p[v[vs - 1].SE].y); } } else { s[v[i].SE] = (st.empty() ? maxp.y : st.top().FI) - p[v[i].SE].y; } maxp.x = max(maxp.x, p[v[i].SE].x + s[v[i].SE]); maxp.y = max(maxp.y, p[v[i].SE].y + s[v[i].SE]); st.push(MP(p[v[i].SE].y, p[v[i].SE].x + s[v[i].SE])); } if (check()) { return true; } return false; } int main() { scanf("%d", &t); REP(tt, t) { scanf("%d", &n); REP(i, n) { scanf("%d%d", &p[i].x, &p[i].y); } bool ok = solve(); if (ok) { printf("TAK"); REP(i, n) { printf(" %d", s[i]); } printf("\n"); } else { printf("NIE\n"); } } return 0; } |