#include <bits/stdc++.h> using namespace std; int n; const int N = 2000; int x[N + 7]; int y[N + 7]; int r[N + 7]; const int inf = 1e9 + 7; int dol; int lewo; struct intv { int a, b, y; intv() : a(0), b(0), y(0) {} intv(int a, int b, int y) : a(a), b(b), y(y) {} }; struct cmp2 { bool operator()(intv a, intv b) { return a.b < b.b; } }; set <intv, cmp2> S; map <pair <int, int>, int> pozycje; int sasiadzdolu(int xi, int yi) { auto it = pozycje.lower_bound({xi, yi + 1});\ if(it == pozycje.begin()) return -1; it--; if((*it).first.first == xi) return (*it).second; return -1; } int sasiadzdoludzialajacy(int xi, int yi) { xi--; yi--; int res = -1; for(int i = 1; i <= n; ++i) { if(x[i] == xi && y[i] <= yi) { if(res == -1) res = i; else if(y[i] > y[res]) res = i; } } if(sasiadzdolu(xi + 1, yi + 1) != res) cout << "sasiadzdolu nie dziala dla " << xi + 1 << ' ' << yi + 1 << endl; return res; } int poziom(int a, int b) { auto ita = S.upper_bound(intv(0, a - 1, 0)); auto itb = S.upper_bound(intv(0, b - 1, 0)); if(ita != itb) return -1; return (*ita).y; } vector <int> czyszczenie; void wstaw(intv x) { auto it = S.upper_bound(intv(0, x.b - 1, 0)); int yl, yr; yl = yr = -1; auto itl = it; auto itr = it; if(itl != S.begin()) { itl--; yl = itl -> y; } itr++; if(itr != S.end()) yr = itr -> y; intv a = *it; S.erase(it); if(yl == yr && yl == x.y && a.a == x.a && a.b == x.b) { int na = itl -> a; int nb = itr -> b; S.erase(itl); S.erase(itr); S.insert(intv(na, nb, x.y)); return; } bool jest = 0; if(a.a < x.a) S.insert(intv(a.a, x.a - 1, a.y)); else if(yl == x.y) { int na = itl -> a; int nb = x.b; S.erase(itl); S.insert(intv(na, nb, x.y)); jest = 1; } if(x.b < a.b) S.insert(intv(x.b + 1, a.b, a.y)); else if(yr == x.y) { int na = x.a; int nb = itr -> b; S.erase(itr); S.insert(intv(na, nb, x.y)); jest = 1; } if(!jest) S.insert(x); } bool sprawdz(int h, int vo) { // cout << "sprawdzam h vo " << h << ' ' << vo << endl; S.clear(); S.insert(intv(1, inf, h)); for(auto u : czyszczenie) r[u] = 0; czyszczenie.clear(); vector <pair <int, int>> rogi; rogi.push_back({lewo, h - 1}); int grp = -1; while(!rogi.empty()) { int xi = rogi.back().first; int yi = rogi.back().second; rogi.pop_back(); // cout << "rog " << xi << ' ' << yi << endl; int w = sasiadzdolu(xi, yi); // cout << "w = " << w << endl; if(w == -1) { if(grp == -1) grp = xi; else if(xi != grp) { // cout << "koncze" << endl; return false; } } else { r[w] = yi - y[w]; czyszczenie.push_back(w); int nh = poziom(xi, xi + r[w] - 1); if(nh == -1) return false; wstaw(intv(xi, xi + r[w] - 1, y[w] + 1)); if(y[w] >= dol && (xi == lewo || poziom(xi - 1, xi - 1) <= y[w])) rogi.push_back({xi, y[w]}); int pp = poziom(xi + r[w], xi + r[w]) - 1; if(pp <= yi && pp > yi - r[w]) rogi.push_back({xi + r[w], pp}); } } // cout << "po rogach " << endl; if(poziom(lewo, grp - 1) != dol || poziom(grp, inf) != h) return false; r[vo] = grp - lewo; czyszczenie.push_back(vo); return true; } void solve() { cin >> n; pozycje.clear(); dol = lewo = inf; for(int i = 1; i <= n; ++i) { cin >> x[i]; cin >> y[i]; dol = min(dol, y[i] + 1); lewo = min(lewo, x[i] + 1); pozycje[{x[i] + 1, y[i] + 1}] = i; } if(n == 1) { cout << "TAK 1\n"; return; } int xmin = inf; int ymax = -1; int v = 0; for(int i = 1; i <= n; ++i) { if(x[i] < xmin) { xmin = x[i]; ymax = y[i]; v = i; } else if(x[i] == xmin && y[i] > ymax) { ymax = y[i]; v = i; } } bool oddziela = 1; for(int i = 1; i <= n; ++i) if(i != v && y[i] >= ymax) oddziela = 0; if(oddziela && sprawdz(ymax + 1, v)) { bool niezera = 1; for(int j = 1; j <= n; ++j) if(r[j] == 0) niezera = 0; if(niezera) { cout << "TAK "; for(int i = 1; i <= n; ++i) cout << r[i] << ' '; cout << endl; } return; } // cout << "v = " << v << endl; for(int i = 1; i <= n; ++i) { if(i != v && x[i] > xmin) { int r1 = x[i] - xmin; int r2 = ymax + r1 - y[i]; // cout << "i r1 r2 " << i << ' ' << r1 << ' ' << r2 << endl; bool ok = 1; for(int j = 1; j <= n && ok; ++j) { if(j == v || j == i) continue; if(x[j] >= xmin && y[j] >= ymax && x[j] < xmin + r1 && y[j] < ymax + r1) ok = 0;//, cout << j << " jest gupie\n"; if(x[j] >= x[i] && y[j] >= y[i] && x[j] < x[i] + r2 && y[j] < y[i] + r2) ok = 0;//, cout << j << " jest gupie2\n"; } if(ok) { if(sprawdz(ymax + 1 + x[i] - xmin, 0)) { bool niezera = 1; for(int j = 1; j <= n; ++j) if(r[j] == 0) niezera = 0; if(niezera) { cout << "TAK "; for(int j = 1; j <= n; ++j) cout << r[j] << ' '; cout << endl; return; } } } } } cout << "NIE " << endl; } int main() { ios_base::sync_with_stdio(0); int tt; cin >> tt; while(tt--) solve(); 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 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 | #include <bits/stdc++.h> using namespace std; int n; const int N = 2000; int x[N + 7]; int y[N + 7]; int r[N + 7]; const int inf = 1e9 + 7; int dol; int lewo; struct intv { int a, b, y; intv() : a(0), b(0), y(0) {} intv(int a, int b, int y) : a(a), b(b), y(y) {} }; struct cmp2 { bool operator()(intv a, intv b) { return a.b < b.b; } }; set <intv, cmp2> S; map <pair <int, int>, int> pozycje; int sasiadzdolu(int xi, int yi) { auto it = pozycje.lower_bound({xi, yi + 1});\ if(it == pozycje.begin()) return -1; it--; if((*it).first.first == xi) return (*it).second; return -1; } int sasiadzdoludzialajacy(int xi, int yi) { xi--; yi--; int res = -1; for(int i = 1; i <= n; ++i) { if(x[i] == xi && y[i] <= yi) { if(res == -1) res = i; else if(y[i] > y[res]) res = i; } } if(sasiadzdolu(xi + 1, yi + 1) != res) cout << "sasiadzdolu nie dziala dla " << xi + 1 << ' ' << yi + 1 << endl; return res; } int poziom(int a, int b) { auto ita = S.upper_bound(intv(0, a - 1, 0)); auto itb = S.upper_bound(intv(0, b - 1, 0)); if(ita != itb) return -1; return (*ita).y; } vector <int> czyszczenie; void wstaw(intv x) { auto it = S.upper_bound(intv(0, x.b - 1, 0)); int yl, yr; yl = yr = -1; auto itl = it; auto itr = it; if(itl != S.begin()) { itl--; yl = itl -> y; } itr++; if(itr != S.end()) yr = itr -> y; intv a = *it; S.erase(it); if(yl == yr && yl == x.y && a.a == x.a && a.b == x.b) { int na = itl -> a; int nb = itr -> b; S.erase(itl); S.erase(itr); S.insert(intv(na, nb, x.y)); return; } bool jest = 0; if(a.a < x.a) S.insert(intv(a.a, x.a - 1, a.y)); else if(yl == x.y) { int na = itl -> a; int nb = x.b; S.erase(itl); S.insert(intv(na, nb, x.y)); jest = 1; } if(x.b < a.b) S.insert(intv(x.b + 1, a.b, a.y)); else if(yr == x.y) { int na = x.a; int nb = itr -> b; S.erase(itr); S.insert(intv(na, nb, x.y)); jest = 1; } if(!jest) S.insert(x); } bool sprawdz(int h, int vo) { // cout << "sprawdzam h vo " << h << ' ' << vo << endl; S.clear(); S.insert(intv(1, inf, h)); for(auto u : czyszczenie) r[u] = 0; czyszczenie.clear(); vector <pair <int, int>> rogi; rogi.push_back({lewo, h - 1}); int grp = -1; while(!rogi.empty()) { int xi = rogi.back().first; int yi = rogi.back().second; rogi.pop_back(); // cout << "rog " << xi << ' ' << yi << endl; int w = sasiadzdolu(xi, yi); // cout << "w = " << w << endl; if(w == -1) { if(grp == -1) grp = xi; else if(xi != grp) { // cout << "koncze" << endl; return false; } } else { r[w] = yi - y[w]; czyszczenie.push_back(w); int nh = poziom(xi, xi + r[w] - 1); if(nh == -1) return false; wstaw(intv(xi, xi + r[w] - 1, y[w] + 1)); if(y[w] >= dol && (xi == lewo || poziom(xi - 1, xi - 1) <= y[w])) rogi.push_back({xi, y[w]}); int pp = poziom(xi + r[w], xi + r[w]) - 1; if(pp <= yi && pp > yi - r[w]) rogi.push_back({xi + r[w], pp}); } } // cout << "po rogach " << endl; if(poziom(lewo, grp - 1) != dol || poziom(grp, inf) != h) return false; r[vo] = grp - lewo; czyszczenie.push_back(vo); return true; } void solve() { cin >> n; pozycje.clear(); dol = lewo = inf; for(int i = 1; i <= n; ++i) { cin >> x[i]; cin >> y[i]; dol = min(dol, y[i] + 1); lewo = min(lewo, x[i] + 1); pozycje[{x[i] + 1, y[i] + 1}] = i; } if(n == 1) { cout << "TAK 1\n"; return; } int xmin = inf; int ymax = -1; int v = 0; for(int i = 1; i <= n; ++i) { if(x[i] < xmin) { xmin = x[i]; ymax = y[i]; v = i; } else if(x[i] == xmin && y[i] > ymax) { ymax = y[i]; v = i; } } bool oddziela = 1; for(int i = 1; i <= n; ++i) if(i != v && y[i] >= ymax) oddziela = 0; if(oddziela && sprawdz(ymax + 1, v)) { bool niezera = 1; for(int j = 1; j <= n; ++j) if(r[j] == 0) niezera = 0; if(niezera) { cout << "TAK "; for(int i = 1; i <= n; ++i) cout << r[i] << ' '; cout << endl; } return; } // cout << "v = " << v << endl; for(int i = 1; i <= n; ++i) { if(i != v && x[i] > xmin) { int r1 = x[i] - xmin; int r2 = ymax + r1 - y[i]; // cout << "i r1 r2 " << i << ' ' << r1 << ' ' << r2 << endl; bool ok = 1; for(int j = 1; j <= n && ok; ++j) { if(j == v || j == i) continue; if(x[j] >= xmin && y[j] >= ymax && x[j] < xmin + r1 && y[j] < ymax + r1) ok = 0;//, cout << j << " jest gupie\n"; if(x[j] >= x[i] && y[j] >= y[i] && x[j] < x[i] + r2 && y[j] < y[i] + r2) ok = 0;//, cout << j << " jest gupie2\n"; } if(ok) { if(sprawdz(ymax + 1 + x[i] - xmin, 0)) { bool niezera = 1; for(int j = 1; j <= n; ++j) if(r[j] == 0) niezera = 0; if(niezera) { cout << "TAK "; for(int j = 1; j <= n; ++j) cout << r[j] << ' '; cout << endl; return; } } } } } cout << "NIE " << endl; } int main() { ios_base::sync_with_stdio(0); int tt; cin >> tt; while(tt--) solve(); return 0; } |