#include <cstdio> #include <algorithm> using namespace std; const int R = 2001; const int INF = 1000000001; struct punkt { int x, y, nr; }t[R]; bool pox(punkt a, punkt b) { if(a.x == b.x)return a.y < b.y; return a.x < b.x; } bool poy(punkt a, punkt b) { if(a.y == b.y)return a.x < b.x; return a.y < b.y; } int x[R], y[R], prawy[R], wyzej[R], odp[R], stos[R]; int main() { int q; scanf("%d",&q); while(q--) { int n, rozm = 0; scanf("%d",&n); int l = INF, d = INF; for(int i=0;i<n;++i) { scanf("%d%d",&x[i],&y[i]); t[i].x = x[i]; t[i].y = y[i]; t[i].nr = i; l = min(l, x[i]); d = min(d, y[i]); } sort(t, t+n, pox); for(int i=0;i<n-1;++i) { if(t[i].x == t[i+1].x)wyzej[t[i].nr] = t[i+1].nr; else wyzej[t[i].nr] = -1; } wyzej[t[n-1].nr] = -1; sort(t, t+n, poy); for(int i=0;i<n-1;++i) { if(t[i].y == t[i+1].y)prawy[t[i].nr] = t[i+1].nr; else prawy[t[i].nr] = -1; } prawy[t[n-1].nr] = -1; int nie = 0; if(t[0].x != l || t[0].y != d)nie = 1; int p = l, g = d; for(int i=0;i<n;++i) { if(prawy[t[i].nr] >= 0) { if(wyzej[t[i].nr] >= 0) { if(x[prawy[t[i].nr]] - x[t[i].nr] == y[wyzej[t[i].nr]] - y[t[i].nr]) odp[t[i].nr] = x[prawy[t[i].nr]] - x[t[i].nr]; else nie = 2; } else odp[t[i].nr] = x[prawy[t[i].nr]] - x[t[i].nr]; } else { if(wyzej[t[i].nr] >= 0)odp[t[i].nr] = y[wyzej[t[i].nr]] - y[t[i].nr]; else stos[rozm++] = t[i].nr; } if(stos[rozm-1] != t[i].nr) { if(g == y[t[i].nr])g += odp[t[i].nr]; else if(g != y[t[i].nr] + odp[t[i].nr])nie = 9; p = max(p, x[t[i].nr] + odp[t[i].nr]); } } if(rozm == 1) { odp[stos[0]] = max(p - x[stos[0]], g - y[stos[0]]); if(odp[stos[0]] == 0)odp[stos[0]] = 1; } else { p = y[stos[1]] - y[stos[0]] + x[stos[0]]; for(int i=0;i<rozm;++i)odp[stos[i]] = p - x[stos[i]]; } if(nie)printf("NIE\n"); else { printf("TAK "); for(int i=0;i<n;++i)printf("%d ",odp[i]); printf("\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 | #include <cstdio> #include <algorithm> using namespace std; const int R = 2001; const int INF = 1000000001; struct punkt { int x, y, nr; }t[R]; bool pox(punkt a, punkt b) { if(a.x == b.x)return a.y < b.y; return a.x < b.x; } bool poy(punkt a, punkt b) { if(a.y == b.y)return a.x < b.x; return a.y < b.y; } int x[R], y[R], prawy[R], wyzej[R], odp[R], stos[R]; int main() { int q; scanf("%d",&q); while(q--) { int n, rozm = 0; scanf("%d",&n); int l = INF, d = INF; for(int i=0;i<n;++i) { scanf("%d%d",&x[i],&y[i]); t[i].x = x[i]; t[i].y = y[i]; t[i].nr = i; l = min(l, x[i]); d = min(d, y[i]); } sort(t, t+n, pox); for(int i=0;i<n-1;++i) { if(t[i].x == t[i+1].x)wyzej[t[i].nr] = t[i+1].nr; else wyzej[t[i].nr] = -1; } wyzej[t[n-1].nr] = -1; sort(t, t+n, poy); for(int i=0;i<n-1;++i) { if(t[i].y == t[i+1].y)prawy[t[i].nr] = t[i+1].nr; else prawy[t[i].nr] = -1; } prawy[t[n-1].nr] = -1; int nie = 0; if(t[0].x != l || t[0].y != d)nie = 1; int p = l, g = d; for(int i=0;i<n;++i) { if(prawy[t[i].nr] >= 0) { if(wyzej[t[i].nr] >= 0) { if(x[prawy[t[i].nr]] - x[t[i].nr] == y[wyzej[t[i].nr]] - y[t[i].nr]) odp[t[i].nr] = x[prawy[t[i].nr]] - x[t[i].nr]; else nie = 2; } else odp[t[i].nr] = x[prawy[t[i].nr]] - x[t[i].nr]; } else { if(wyzej[t[i].nr] >= 0)odp[t[i].nr] = y[wyzej[t[i].nr]] - y[t[i].nr]; else stos[rozm++] = t[i].nr; } if(stos[rozm-1] != t[i].nr) { if(g == y[t[i].nr])g += odp[t[i].nr]; else if(g != y[t[i].nr] + odp[t[i].nr])nie = 9; p = max(p, x[t[i].nr] + odp[t[i].nr]); } } if(rozm == 1) { odp[stos[0]] = max(p - x[stos[0]], g - y[stos[0]]); if(odp[stos[0]] == 0)odp[stos[0]] = 1; } else { p = y[stos[1]] - y[stos[0]] + x[stos[0]]; for(int i=0;i<rozm;++i)odp[stos[i]] = p - x[stos[i]]; } if(nie)printf("NIE\n"); else { printf("TAK "); for(int i=0;i<n;++i)printf("%d ",odp[i]); printf("\n"); } } return 0; } |