#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair < int, int > PII; typedef pair < LL, LL > PLL; typedef pair < LD, LD > PDD; #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() template < typename _T > inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template < typename _T, typename... args > void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #ifdef LOCAL #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) #else #define DBG(...) (__VA_ARGS__) #define cerr if(0) cout #endif // ********************** CODE ********************** // const int N = 2e3 + 7; const int INF = 1e12 + 7; int n; LL ans[N]; pair < PLL, int > T[N]; inline bool in(int &a, int &b) { return T[a].first.first <= T[b].first.first && T[a].first.second <= T[b].first.second; } #define wa { puts("NIE"); return; } void solve() { scanf("%d", &n); PLL mn = {INF, INF}, mx = {0, 0}; for(int i = 1; i <= n; i++) { scanf("%lld%lld", &T[i].first.first, &T[i].first.second); T[i].second = i; mn.first = min(mn.first, T[i].first.first); mn.second = min(mn.second, T[i].first.second); } for(int i = 1; i <= n; i++) { T[i].first.first -= mn.first; T[i].first.second -= mn.second; } sort(T + 1, T + n + 1); if(T[1].first != make_pair(0LL, 0LL)) wa if(n == 1) { printf("TAK 1\n"); return; } int id = 1; for(int i = 2; i <= n; i++) if(T[i].first.first == T[1].first.first) id = i; LL mxY = T[id].first.second, pole = 0; bool ok = 1; mx = {0, 0}; for(int i = 1; i <= n; i++) { if(i == id) continue; LL dl = mxY - T[i].first.second; for(int j = 1; j < i; j++) { if(j == id) continue; if(T[j].first.second <= T[i].first.second && T[i].first.first < T[j].first.first + ans[T[j].second] && T[i].first.second < T[j].first.second + ans[T[j].second]) { ok = 0; break; } } if(!ok) break; for(int j = i + 1; j <= n; j++) { if(j == id) continue; if(T[i].first.first != T[j].first.first) break; if(in(i, j)) { dl = min(dl, max(T[j].first.first - T[i].first.first, T[j].first.second - T[i].first.second)); } } for(int j = 1; j < i; j++) { if(j == id) continue; LL cp = T[j].first.first; T[j].first.first = min(cp + ans[T[j].second] - 1, T[i].first.first); if(in(i, j)) { dl = min(dl, max(T[j].first.first - T[i].first.first, T[j].first.second - T[i].first.second)); } T[j].first.first = cp; } if(dl <= 0) { ok = 0; break; } ans[T[i].second] = dl; mx.first = max(mx.first, T[i].first.first + dl); mx.second = max(mx.second, T[i].first.second + dl); pole += LL(dl) * dl; } if(ok && mxY * mx.first == pole) { printf("TAK "); ans[T[id].second] = mx.first; for(int i = 1; i <= n; i++) printf("%lld ", ans[i]); puts(""); return; } if(id == n) wa vector < int > can; for(int i = id + 1; i <= n; i++) { mxY = T[id].first.second + T[i].first.first - T[id].first.first; ok = 1; LL wym = mxY - T[i].first.second; if(wym > 0 && mxY > T[id].first.second && mxY > T[i].first.second) { for(int j = 1; j <= n; j++) { if(j == id || j == i) continue; if(T[j].first.second >= mxY) { ok = 0; break; } if(T[id].first.first <= T[j].first.first && T[j].first.first < T[i].first.first && T[j].first.second >= T[id].first.second) { ok = 0; break; } if(T[i].first.first <= T[j].first.first && T[j].first.first < T[i].first.first + wym && T[i].first.second <= T[j].first.second) { ok = 0; break; } } if(ok) can.push_back(mxY); } } sort(all(can)); can.erase(unique(all(can)), can.end()); for(auto mxY2: can) { mxY = mxY2, pole = 0; ok = 1; mx = {0, 0}; for(int i = 1; i <= n; i++) { LL dl = mxY - T[i].first.second; for(int j = 1; j < i; j++) { if(T[j].first.second <= T[i].first.second && T[i].first.first < T[j].first.first + ans[T[j].second] && T[i].first.second < T[j].first.second + ans[T[j].second]) { ok = 0; break; } } if(!ok) break; for(int j = i + 1; j <= n; j++) { if(T[i].first.first != T[j].first.first) break; if(in(i, j)) { dl = min(dl, max(T[j].first.first - T[i].first.first, T[j].first.second - T[i].first.second)); } } for(int j = 1; j < i; j++) { LL cp = T[j].first.first; T[j].first.first = min(cp + ans[T[j].second] - 1, T[i].first.first); //T[j].first.first += ans[T[j].second] - 1; if(in(i, j)) { dl = min(dl, max(T[j].first.first - T[i].first.first, T[j].first.second - T[i].first.second)); } //T[j].first.first -= ans[T[j].second] - 1; T[j].first.first = cp; } if(dl <= 0) { ok = 0; break; } ans[T[i].second] = dl; mx.first = max(mx.first, T[i].first.first + dl); mx.second = max(mx.second, T[i].first.second + dl); pole += LL(dl) * dl; } assert(mx.second == mxY); if(ok && mxY * mx.first == pole) { printf("TAK "); for(int i = 1; i <= n; i++) printf("%lld ", ans[i]); puts(""); return; } } puts("NIE"); } int main() { int te; scanf("%d", &te); while(te--) { 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 | #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef pair < int, int > PII; typedef pair < LL, LL > PLL; typedef pair < LD, LD > PDD; #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() template < typename _T > inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template < typename _T, typename... args > void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } #ifdef LOCAL #define DBG(...) _DBG(#__VA_ARGS__, __VA_ARGS__) #else #define DBG(...) (__VA_ARGS__) #define cerr if(0) cout #endif // ********************** CODE ********************** // const int N = 2e3 + 7; const int INF = 1e12 + 7; int n; LL ans[N]; pair < PLL, int > T[N]; inline bool in(int &a, int &b) { return T[a].first.first <= T[b].first.first && T[a].first.second <= T[b].first.second; } #define wa { puts("NIE"); return; } void solve() { scanf("%d", &n); PLL mn = {INF, INF}, mx = {0, 0}; for(int i = 1; i <= n; i++) { scanf("%lld%lld", &T[i].first.first, &T[i].first.second); T[i].second = i; mn.first = min(mn.first, T[i].first.first); mn.second = min(mn.second, T[i].first.second); } for(int i = 1; i <= n; i++) { T[i].first.first -= mn.first; T[i].first.second -= mn.second; } sort(T + 1, T + n + 1); if(T[1].first != make_pair(0LL, 0LL)) wa if(n == 1) { printf("TAK 1\n"); return; } int id = 1; for(int i = 2; i <= n; i++) if(T[i].first.first == T[1].first.first) id = i; LL mxY = T[id].first.second, pole = 0; bool ok = 1; mx = {0, 0}; for(int i = 1; i <= n; i++) { if(i == id) continue; LL dl = mxY - T[i].first.second; for(int j = 1; j < i; j++) { if(j == id) continue; if(T[j].first.second <= T[i].first.second && T[i].first.first < T[j].first.first + ans[T[j].second] && T[i].first.second < T[j].first.second + ans[T[j].second]) { ok = 0; break; } } if(!ok) break; for(int j = i + 1; j <= n; j++) { if(j == id) continue; if(T[i].first.first != T[j].first.first) break; if(in(i, j)) { dl = min(dl, max(T[j].first.first - T[i].first.first, T[j].first.second - T[i].first.second)); } } for(int j = 1; j < i; j++) { if(j == id) continue; LL cp = T[j].first.first; T[j].first.first = min(cp + ans[T[j].second] - 1, T[i].first.first); if(in(i, j)) { dl = min(dl, max(T[j].first.first - T[i].first.first, T[j].first.second - T[i].first.second)); } T[j].first.first = cp; } if(dl <= 0) { ok = 0; break; } ans[T[i].second] = dl; mx.first = max(mx.first, T[i].first.first + dl); mx.second = max(mx.second, T[i].first.second + dl); pole += LL(dl) * dl; } if(ok && mxY * mx.first == pole) { printf("TAK "); ans[T[id].second] = mx.first; for(int i = 1; i <= n; i++) printf("%lld ", ans[i]); puts(""); return; } if(id == n) wa vector < int > can; for(int i = id + 1; i <= n; i++) { mxY = T[id].first.second + T[i].first.first - T[id].first.first; ok = 1; LL wym = mxY - T[i].first.second; if(wym > 0 && mxY > T[id].first.second && mxY > T[i].first.second) { for(int j = 1; j <= n; j++) { if(j == id || j == i) continue; if(T[j].first.second >= mxY) { ok = 0; break; } if(T[id].first.first <= T[j].first.first && T[j].first.first < T[i].first.first && T[j].first.second >= T[id].first.second) { ok = 0; break; } if(T[i].first.first <= T[j].first.first && T[j].first.first < T[i].first.first + wym && T[i].first.second <= T[j].first.second) { ok = 0; break; } } if(ok) can.push_back(mxY); } } sort(all(can)); can.erase(unique(all(can)), can.end()); for(auto mxY2: can) { mxY = mxY2, pole = 0; ok = 1; mx = {0, 0}; for(int i = 1; i <= n; i++) { LL dl = mxY - T[i].first.second; for(int j = 1; j < i; j++) { if(T[j].first.second <= T[i].first.second && T[i].first.first < T[j].first.first + ans[T[j].second] && T[i].first.second < T[j].first.second + ans[T[j].second]) { ok = 0; break; } } if(!ok) break; for(int j = i + 1; j <= n; j++) { if(T[i].first.first != T[j].first.first) break; if(in(i, j)) { dl = min(dl, max(T[j].first.first - T[i].first.first, T[j].first.second - T[i].first.second)); } } for(int j = 1; j < i; j++) { LL cp = T[j].first.first; T[j].first.first = min(cp + ans[T[j].second] - 1, T[i].first.first); //T[j].first.first += ans[T[j].second] - 1; if(in(i, j)) { dl = min(dl, max(T[j].first.first - T[i].first.first, T[j].first.second - T[i].first.second)); } //T[j].first.first -= ans[T[j].second] - 1; T[j].first.first = cp; } if(dl <= 0) { ok = 0; break; } ans[T[i].second] = dl; mx.first = max(mx.first, T[i].first.first + dl); mx.second = max(mx.second, T[i].first.second + dl); pole += LL(dl) * dl; } assert(mx.second == mxY); if(ok && mxY * mx.first == pole) { printf("TAK "); for(int i = 1; i <= n; i++) printf("%lld ", ans[i]); puts(""); return; } } puts("NIE"); } int main() { int te; scanf("%d", &te); while(te--) { solve(); } return 0; } |