#include <bits/stdc++.h> using namespace std; #define x first #define y second typedef pair<uint, uint> point; const int N = 2001; const uint inf = 4e9; point p[N]; vector<pair<uint, int>> pts[N]; vector<pair<uint, uint>> rem[N]; uint ans[N]; int main() { int test; scanf("%d", &test); while(test--) { int n; scanf("%d", &n); for(int i = 0; i <= n; i++) pts[i].clear(); vector<uint> x; uint low = inf, mx = 0; for(int i = 0; i < n; i++) { scanf("%u %u", &p[i].x, &p[i].y); low = min(low, p[i].y); mx = max(mx, p[i].y); x.push_back(p[i].x); } if(n == 1) { puts("TAK 1"); continue; } sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end()); for(int i = 0; i < n; i++) { int nr = lower_bound(x.begin(), x.end(), p[i].x) - x.begin(); pts[nr].emplace_back(p[i].y, i); } if(x.size() == 1) { sort(pts[0].begin(), pts[0].end()); uint d = pts[0][1].x - pts[0][0].x; bool ok = true; for(int i = 1; i < pts[0].size(); i++) if(pts[0][i].x - pts[0][i-1].x != d) ok = false; if(ok) { printf("TAK"); for(int i = 0; i < n; i++) printf(" %u", d); puts(""); } else puts("NIE"); } else { for(int i = 0; i < x.size(); i++) { sort(pts[i].begin(), pts[i].end()); reverse(pts[i].begin(), pts[i].end()); } bool success = false; auto lt = pts[0][0]; for(int i = 0; i < x.size(); i++) { for(int j = 0; j < x.size(); j++) rem[j].clear(); uint high = lt.first + x[i] - x[0]; if(mx > high) continue; set<pair<uint, uint>> s = { { low, low }, { high, high+1 } }; set<int> end; bool ok = true; uint cov = 0, endcov = 0; for(int j = 0; ok && j < x.size(); j++) { for(auto pr: rem[j]) { cov -= pr.second - pr.first; s.erase(pr); } for(auto pr: pts[j]) { auto it = s.upper_bound({pr.first, pr.first}); if(prev(it)->second > pr.first) { ok = false; break; } int top = it->first; int nr = pr.second; point pp = p[nr]; uint a = top - pp.y; if(a == 0 && nr != lt.second) { ok = false; break; } ans[nr] = a; s.insert({pp.y, pp.y + a}); cov += a; auto xit = lower_bound(x.begin(), x.end(), pp.x + a); if(xit == x.end() || *xit != pp.x + a) { end.insert(pp.x + a); endcov += a; } else rem[xit-x.begin()].emplace_back(pp.y, pp.y + a); } if(cov != high - low) { ok = false; } } if(end.size() != 1 || endcov != high - low) { ok = false; } if(ok) { if(i == 0) ans[lt.second] = *end.begin() - x[0]; printf("TAK"); for(int j = 0; j < n; j++) printf(" %u", ans[j]); success = true; puts(""); break; } } if(!success) 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 | #include <bits/stdc++.h> using namespace std; #define x first #define y second typedef pair<uint, uint> point; const int N = 2001; const uint inf = 4e9; point p[N]; vector<pair<uint, int>> pts[N]; vector<pair<uint, uint>> rem[N]; uint ans[N]; int main() { int test; scanf("%d", &test); while(test--) { int n; scanf("%d", &n); for(int i = 0; i <= n; i++) pts[i].clear(); vector<uint> x; uint low = inf, mx = 0; for(int i = 0; i < n; i++) { scanf("%u %u", &p[i].x, &p[i].y); low = min(low, p[i].y); mx = max(mx, p[i].y); x.push_back(p[i].x); } if(n == 1) { puts("TAK 1"); continue; } sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end()); for(int i = 0; i < n; i++) { int nr = lower_bound(x.begin(), x.end(), p[i].x) - x.begin(); pts[nr].emplace_back(p[i].y, i); } if(x.size() == 1) { sort(pts[0].begin(), pts[0].end()); uint d = pts[0][1].x - pts[0][0].x; bool ok = true; for(int i = 1; i < pts[0].size(); i++) if(pts[0][i].x - pts[0][i-1].x != d) ok = false; if(ok) { printf("TAK"); for(int i = 0; i < n; i++) printf(" %u", d); puts(""); } else puts("NIE"); } else { for(int i = 0; i < x.size(); i++) { sort(pts[i].begin(), pts[i].end()); reverse(pts[i].begin(), pts[i].end()); } bool success = false; auto lt = pts[0][0]; for(int i = 0; i < x.size(); i++) { for(int j = 0; j < x.size(); j++) rem[j].clear(); uint high = lt.first + x[i] - x[0]; if(mx > high) continue; set<pair<uint, uint>> s = { { low, low }, { high, high+1 } }; set<int> end; bool ok = true; uint cov = 0, endcov = 0; for(int j = 0; ok && j < x.size(); j++) { for(auto pr: rem[j]) { cov -= pr.second - pr.first; s.erase(pr); } for(auto pr: pts[j]) { auto it = s.upper_bound({pr.first, pr.first}); if(prev(it)->second > pr.first) { ok = false; break; } int top = it->first; int nr = pr.second; point pp = p[nr]; uint a = top - pp.y; if(a == 0 && nr != lt.second) { ok = false; break; } ans[nr] = a; s.insert({pp.y, pp.y + a}); cov += a; auto xit = lower_bound(x.begin(), x.end(), pp.x + a); if(xit == x.end() || *xit != pp.x + a) { end.insert(pp.x + a); endcov += a; } else rem[xit-x.begin()].emplace_back(pp.y, pp.y + a); } if(cov != high - low) { ok = false; } } if(end.size() != 1 || endcov != high - low) { ok = false; } if(ok) { if(i == 0) ans[lt.second] = *end.begin() - x[0]; printf("TAK"); for(int j = 0; j < n; j++) printf(" %u", ans[j]); success = true; puts(""); break; } } if(!success) puts("NIE"); } } } |