#include <algorithm> #include <cstdio> #include <cstring> #include <map> #include <queue> #include <vector> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define VAR(v,w) __typeof(w) v=(w) #define PB push_back #define ALL(c) (c).begin(),(c).end() #define MP make_pair #define FT first #define SD second #define INT(x) int x; scanf("%d", &x) typedef pair<int,int> PII; typedef vector<int> VI; int n; int x[2000], y[2000]; int v[2000], h[2000]; bool support[2000]; int rv[2000]; map<int, int> wall; priority_queue<PII> qwall; int res[2000]; bool compV(int i, int j) { if (y[i] != y[j]) return y[i] < y[j]; return x[i] > x[j]; } bool compH(int i, int j) { if (x[i] != x[j]) return x[i] < x[j]; return y[i] > y[j]; } struct Seg { int x1, x2, y; Seg(int x1, int x2, int y) : x1(x1), x2(x2), y(y) {} bool operator<(const Seg& that) const { if (y != that.y) return y < that.y; return x1 < that.x1; } }; bool verify() { vector<Seg> starts, ends; REP(i,n) { starts.PB(Seg(x[i], x[i] + res[i], y[i])); ends.PB(Seg(x[i], x[i] + res[i], y[i] + res[i])); } starts.PB(Seg(x[v[n - 1]], x[v[0]] + res[v[0]], y[v[n - 1]] + res[v[n - 1]])); ends.PB(Seg(x[v[n - 1]], x[v[0]] + res[v[0]], y[v[0]])); sort(ALL(starts)); sort(ALL(ends)); int is = 0, ie = 0; while (is < n + 1 && ie < n + 1) { if (starts[is].y != ends[ie].y) return 0; int yy = starts[is].y; VI p1, p2; while (is < n + 1 && starts[is].y == yy) { if (!p1.empty() && p1.back() > starts[is].x1) return 0; if (!p1.empty() && p1.back() == starts[is].x1) p1.back() = starts[is].x2; else { p1.PB(starts[is].x1); p1.PB(starts[is].x2); } ++is; } while (ie < n + 1 && ends[ie].y == yy) { if (!p2.empty() && p2.back() > ends[ie].x1) return 0; if (!p2.empty() && p2.back() == ends[ie].x1) p2.back() = ends[ie].x2; else { p2.PB(ends[ie].x1); p2.PB(ends[ie].x2); } ++ie; } if (p1 != p2) return 0; } return is == n + 1 && ie == n + 1; } void oneCase() { INT(n1); n = n1; REP(i,n) scanf("%d%d", &x[i], &y[i]); if (n == 1) { printf("TAK\n1\n"); return; } REP(i,n) v[i] = h[i] = i; sort(v, v + n, compV); sort(h, h + n, compH); int xmi = 2100000000, ymi = 2100000000; REP(i,n) xmi = min(xmi, x[i]); REP(i,n) ymi = min(ymi, y[i]); memset(support, 0, n * sizeof(bool)); bool corner = 0; REP(i,n) if (x[i] == xmi && y[i] == ymi) { corner = 1; support[i] = 1; } if (!corner) { printf("NIE\n"); return; } FOR(i,1,n) if (y[v[i - 1]] == y[v[i]]) support[v[i - 1]] = 1; FOR(i,1,n) if (x[h[i - 1]] == x[h[i]]) support[h[i - 1]] = 1; REP(i,n) if (!support[i]) { printf("NIE\n"); return; } REP(i,n) rv[v[i]] = i; int last = y[v[0]] - 1; REP(q,n) { if (y[v[q]] == last) continue; last = y[v[q]]; int r = x[v[0]] + y[v[q]] - y[v[0]]; wall.clear(); while (!qwall.empty()) qwall.pop(); wall[r] = 2100000000; bool good = 1; REP(iv,n) { if (!iv && r == x[v[0]]) continue; int i = v[iv]; while (!qwall.empty() && -qwall.top().FT <= y[i]) { wall.erase(qwall.top().SD); qwall.pop(); } VAR(it,wall.lower_bound(x[i])); if (it == wall.end() || it->FT == x[i]) { good = 0; break; } res[i] = it->FT - x[i]; int yy = y[i] + res[i]; wall[x[i]] = yy; qwall.push(MP(-yy, x[i])); } if (r == x[v[0]]) { res[v[0]] = res[v[n - 1]] + y[v[n - 1]] - y[v[0]]; if (res[v[0]] > 1000000000) good = 0; else r += res[v[0]]; } if (!good) continue; if (!verify()) continue; REP(i,n) { if (i) printf(" "); else printf("TAK\n"); printf("%d", res[i]); } printf("\n"); return; } printf("NIE\n"); } int main() { INT(t); REP(tt,t) oneCase(); }
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 | #include <algorithm> #include <cstdio> #include <cstring> #include <map> #include <queue> #include <vector> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define VAR(v,w) __typeof(w) v=(w) #define PB push_back #define ALL(c) (c).begin(),(c).end() #define MP make_pair #define FT first #define SD second #define INT(x) int x; scanf("%d", &x) typedef pair<int,int> PII; typedef vector<int> VI; int n; int x[2000], y[2000]; int v[2000], h[2000]; bool support[2000]; int rv[2000]; map<int, int> wall; priority_queue<PII> qwall; int res[2000]; bool compV(int i, int j) { if (y[i] != y[j]) return y[i] < y[j]; return x[i] > x[j]; } bool compH(int i, int j) { if (x[i] != x[j]) return x[i] < x[j]; return y[i] > y[j]; } struct Seg { int x1, x2, y; Seg(int x1, int x2, int y) : x1(x1), x2(x2), y(y) {} bool operator<(const Seg& that) const { if (y != that.y) return y < that.y; return x1 < that.x1; } }; bool verify() { vector<Seg> starts, ends; REP(i,n) { starts.PB(Seg(x[i], x[i] + res[i], y[i])); ends.PB(Seg(x[i], x[i] + res[i], y[i] + res[i])); } starts.PB(Seg(x[v[n - 1]], x[v[0]] + res[v[0]], y[v[n - 1]] + res[v[n - 1]])); ends.PB(Seg(x[v[n - 1]], x[v[0]] + res[v[0]], y[v[0]])); sort(ALL(starts)); sort(ALL(ends)); int is = 0, ie = 0; while (is < n + 1 && ie < n + 1) { if (starts[is].y != ends[ie].y) return 0; int yy = starts[is].y; VI p1, p2; while (is < n + 1 && starts[is].y == yy) { if (!p1.empty() && p1.back() > starts[is].x1) return 0; if (!p1.empty() && p1.back() == starts[is].x1) p1.back() = starts[is].x2; else { p1.PB(starts[is].x1); p1.PB(starts[is].x2); } ++is; } while (ie < n + 1 && ends[ie].y == yy) { if (!p2.empty() && p2.back() > ends[ie].x1) return 0; if (!p2.empty() && p2.back() == ends[ie].x1) p2.back() = ends[ie].x2; else { p2.PB(ends[ie].x1); p2.PB(ends[ie].x2); } ++ie; } if (p1 != p2) return 0; } return is == n + 1 && ie == n + 1; } void oneCase() { INT(n1); n = n1; REP(i,n) scanf("%d%d", &x[i], &y[i]); if (n == 1) { printf("TAK\n1\n"); return; } REP(i,n) v[i] = h[i] = i; sort(v, v + n, compV); sort(h, h + n, compH); int xmi = 2100000000, ymi = 2100000000; REP(i,n) xmi = min(xmi, x[i]); REP(i,n) ymi = min(ymi, y[i]); memset(support, 0, n * sizeof(bool)); bool corner = 0; REP(i,n) if (x[i] == xmi && y[i] == ymi) { corner = 1; support[i] = 1; } if (!corner) { printf("NIE\n"); return; } FOR(i,1,n) if (y[v[i - 1]] == y[v[i]]) support[v[i - 1]] = 1; FOR(i,1,n) if (x[h[i - 1]] == x[h[i]]) support[h[i - 1]] = 1; REP(i,n) if (!support[i]) { printf("NIE\n"); return; } REP(i,n) rv[v[i]] = i; int last = y[v[0]] - 1; REP(q,n) { if (y[v[q]] == last) continue; last = y[v[q]]; int r = x[v[0]] + y[v[q]] - y[v[0]]; wall.clear(); while (!qwall.empty()) qwall.pop(); wall[r] = 2100000000; bool good = 1; REP(iv,n) { if (!iv && r == x[v[0]]) continue; int i = v[iv]; while (!qwall.empty() && -qwall.top().FT <= y[i]) { wall.erase(qwall.top().SD); qwall.pop(); } VAR(it,wall.lower_bound(x[i])); if (it == wall.end() || it->FT == x[i]) { good = 0; break; } res[i] = it->FT - x[i]; int yy = y[i] + res[i]; wall[x[i]] = yy; qwall.push(MP(-yy, x[i])); } if (r == x[v[0]]) { res[v[0]] = res[v[n - 1]] + y[v[n - 1]] - y[v[0]]; if (res[v[0]] > 1000000000) good = 0; else r += res[v[0]]; } if (!good) continue; if (!verify()) continue; REP(i,n) { if (i) printf(" "); else printf("TAK\n"); printf("%d", res[i]); } printf("\n"); return; } printf("NIE\n"); } int main() { INT(t); REP(tt,t) oneCase(); } |