#include<bits/stdc++.h> #define VAR(i,n) __typeof(n) i = (n) #define loop(i,j,s) for(int i=j;i<s;i++) #define loopback(i,j,s) for(int i=j;i>=s;i--) #define foreach(i,c) for(VAR(i,(c).begin());i!=(c).end();i++) #define pln( x ) cout << x << "\n" #define ps( x ) cout << x << " " #define entr cout << "\n" #define pcnt(i) __builtin_popcount(i) #define ll long long #define pb push_back #define mp make_pair #define ff first #define ss second #define SIZE(c) (c).size() #define ALL(c) (c).begin(), (c).end() using namespace std; typedef vector<int> VI; const ll INFTY=3000000000; const int MAX=2100; const int MOD=10000000; void coutTab(int* tab,int n){ loop(i,0,n){ cout<<tab[i]<<" "; } cout<<"\n"; } //------------------------------------------ //Point definition struct Point{ //point ll x,y; ll m; int i; }; Point mp(ll xi,ll yi,int ii){ Point p; p.x=xi; p.y=yi; p.i=ii; p.m=-1; return p; } bool sortByX(Point a,Point b){ if(a.x==b.x) return a.y<b.y; return a.x<b.x; } bool sortByY(Point a,Point b){ if(a.y==b.y) return a.x<b.x; return a.y<b.y; } bool sortByI(Point a,Point b){ return a.i<b.i; } int n; Point ps[MAX]; ll mix=INFTY,miy=INFTY; //max x, max h void coutPoints(){ pln("Points:"); loop(i,0,n){ ps(ps[i].x);ps(ps[i].y);ps(ps[i].m);pln(ps[i].i); } } bool isLast(int j){ if(j+1==n||ps[j+1].y>ps[j].y) return true; else return false; } bool isLastX(int j){ if(j+1==n||ps[j+1].x>ps[j].x) return true; else return false; } ll findM(int j){ //find max possible m ll m=INFTY; loop(i,0,n){ if(i<j){ if(ps[i].x>ps[j].x&&ps[i].y+ps[i].m>ps[j].y&&ps[i].m!=INFTY){ m=min(m,ps[i].x-ps[j].x); } }else if(i>j){ if(ps[i].x>=ps[j].x&&ps[i].y>=ps[j].y){ m=min(m,max(ps[i].x-ps[j].x,ps[i].y-ps[j].y)); } } } return m; } void updateInfty(ll mh,ll me){ ps[n-1].m=min(ps[n-1].m,max(mh-ps[n-1].y,me-ps[n-1].x)); loopback(j,n-2,0){ if(ps[j].m==INFTY){ loop(i,j+1,n){ if(ps[i].x+ps[i].m>ps[j].x){ ps[j].m=min(ps[j].m,ps[i].y-ps[j].y); } } ps[j].m=min(ps[j].m,mh-ps[j].y); mh=max(mh,ps[j].y+ps[j].m); me=max(me,ps[j].x+ps[j].m); } } } bool checkX(ll r, ll me){ ll lc=-1; ll c=mix; while(c!=lc){ lc=c; loop(i,0,n){ if(ps[i].x<=c&&ps[i].x+ps[i].m>c&&ps[i].y<=r&&ps[i].y+ps[i].m>r) c=ps[i].x+ps[i].m; } } if(c==me) return true; else return false; } bool checkY(ll c, ll mh){ ll lr=-1; ll r=miy; //ps(r); while(r!=lr){ lr=r; loop(i,0,n){ if(ps[i].x<=c&&ps[i].x+ps[i].m>c&&ps[i].y<=r&&ps[i].y+ps[i].m>r) r=ps[i].y+ps[i].m; } //ps(r); } if(r>=mh) return true; //ps(c);pln(r); return false; } bool check(ll me,ll mh){ sort(ps,ps+n,sortByX); loop(i,0,n){ if(isLastX(i)) if(!checkY(ps[i].x,mh)){ return false;} } sort(ps,ps+n,sortByY); loop(i,0,n){ if(isLast(i)) if(!checkX(ps[i].y,me)) return false; } return true; } Point psb[MAX]; bool gen(int si,ll me, ll mh){ //ps(si);ps(me);pln(mh); loop(i,si,n){ if(isLast(i)){ ll tmp=findM(i); if(tmp!=INFTY){ ps[i].m=tmp; loop(j,0,n) psb[j]=ps[j]; if(gen(i+1,max(me,ps[i].x+ps[i].m),max(mh,ps[i].y+ps[i].m))) return true; //ps(si);ps(me);pln(mh); loop(j,0,n) ps[j]=psb[j]; ps[i].m=INFTY; }else{ ps[i].m=INFTY; } }else{ ps[i].m=findM(i); mh=max(mh,ps[i].y+ps[i].m); me=max(me,ps[i].x+ps[i].m); } } //coutPoints(); updateInfty(mh,me); //coutPoints(); if(check(me,mh)) return true; return false; } int main(){ ios_base::sync_with_stdio(0); int t; cin>>t; while(t--){ cin>>n; ll x,y,me=0,mh=0; mix=INFTY;miy=INFTY; loop(i,0,n){ cin>>x>>y; me=max(me,x+1); mix=min(x,mix); miy=min(y,miy); mh=max(mh,y+1); ps[i]=mp(x,y,i); } sort(ps,ps+n,sortByY); if(gen(0,me,mh)){ sort(ps,ps+n,sortByI); ps("TAK"); loop(i,0,n){ if(i==n-1) pln(ps[i].m); else ps(ps[i].m); } }else{ pln("NIE"); } //entr; } }
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 | #include<bits/stdc++.h> #define VAR(i,n) __typeof(n) i = (n) #define loop(i,j,s) for(int i=j;i<s;i++) #define loopback(i,j,s) for(int i=j;i>=s;i--) #define foreach(i,c) for(VAR(i,(c).begin());i!=(c).end();i++) #define pln( x ) cout << x << "\n" #define ps( x ) cout << x << " " #define entr cout << "\n" #define pcnt(i) __builtin_popcount(i) #define ll long long #define pb push_back #define mp make_pair #define ff first #define ss second #define SIZE(c) (c).size() #define ALL(c) (c).begin(), (c).end() using namespace std; typedef vector<int> VI; const ll INFTY=3000000000; const int MAX=2100; const int MOD=10000000; void coutTab(int* tab,int n){ loop(i,0,n){ cout<<tab[i]<<" "; } cout<<"\n"; } //------------------------------------------ //Point definition struct Point{ //point ll x,y; ll m; int i; }; Point mp(ll xi,ll yi,int ii){ Point p; p.x=xi; p.y=yi; p.i=ii; p.m=-1; return p; } bool sortByX(Point a,Point b){ if(a.x==b.x) return a.y<b.y; return a.x<b.x; } bool sortByY(Point a,Point b){ if(a.y==b.y) return a.x<b.x; return a.y<b.y; } bool sortByI(Point a,Point b){ return a.i<b.i; } int n; Point ps[MAX]; ll mix=INFTY,miy=INFTY; //max x, max h void coutPoints(){ pln("Points:"); loop(i,0,n){ ps(ps[i].x);ps(ps[i].y);ps(ps[i].m);pln(ps[i].i); } } bool isLast(int j){ if(j+1==n||ps[j+1].y>ps[j].y) return true; else return false; } bool isLastX(int j){ if(j+1==n||ps[j+1].x>ps[j].x) return true; else return false; } ll findM(int j){ //find max possible m ll m=INFTY; loop(i,0,n){ if(i<j){ if(ps[i].x>ps[j].x&&ps[i].y+ps[i].m>ps[j].y&&ps[i].m!=INFTY){ m=min(m,ps[i].x-ps[j].x); } }else if(i>j){ if(ps[i].x>=ps[j].x&&ps[i].y>=ps[j].y){ m=min(m,max(ps[i].x-ps[j].x,ps[i].y-ps[j].y)); } } } return m; } void updateInfty(ll mh,ll me){ ps[n-1].m=min(ps[n-1].m,max(mh-ps[n-1].y,me-ps[n-1].x)); loopback(j,n-2,0){ if(ps[j].m==INFTY){ loop(i,j+1,n){ if(ps[i].x+ps[i].m>ps[j].x){ ps[j].m=min(ps[j].m,ps[i].y-ps[j].y); } } ps[j].m=min(ps[j].m,mh-ps[j].y); mh=max(mh,ps[j].y+ps[j].m); me=max(me,ps[j].x+ps[j].m); } } } bool checkX(ll r, ll me){ ll lc=-1; ll c=mix; while(c!=lc){ lc=c; loop(i,0,n){ if(ps[i].x<=c&&ps[i].x+ps[i].m>c&&ps[i].y<=r&&ps[i].y+ps[i].m>r) c=ps[i].x+ps[i].m; } } if(c==me) return true; else return false; } bool checkY(ll c, ll mh){ ll lr=-1; ll r=miy; //ps(r); while(r!=lr){ lr=r; loop(i,0,n){ if(ps[i].x<=c&&ps[i].x+ps[i].m>c&&ps[i].y<=r&&ps[i].y+ps[i].m>r) r=ps[i].y+ps[i].m; } //ps(r); } if(r>=mh) return true; //ps(c);pln(r); return false; } bool check(ll me,ll mh){ sort(ps,ps+n,sortByX); loop(i,0,n){ if(isLastX(i)) if(!checkY(ps[i].x,mh)){ return false;} } sort(ps,ps+n,sortByY); loop(i,0,n){ if(isLast(i)) if(!checkX(ps[i].y,me)) return false; } return true; } Point psb[MAX]; bool gen(int si,ll me, ll mh){ //ps(si);ps(me);pln(mh); loop(i,si,n){ if(isLast(i)){ ll tmp=findM(i); if(tmp!=INFTY){ ps[i].m=tmp; loop(j,0,n) psb[j]=ps[j]; if(gen(i+1,max(me,ps[i].x+ps[i].m),max(mh,ps[i].y+ps[i].m))) return true; //ps(si);ps(me);pln(mh); loop(j,0,n) ps[j]=psb[j]; ps[i].m=INFTY; }else{ ps[i].m=INFTY; } }else{ ps[i].m=findM(i); mh=max(mh,ps[i].y+ps[i].m); me=max(me,ps[i].x+ps[i].m); } } //coutPoints(); updateInfty(mh,me); //coutPoints(); if(check(me,mh)) return true; return false; } int main(){ ios_base::sync_with_stdio(0); int t; cin>>t; while(t--){ cin>>n; ll x,y,me=0,mh=0; mix=INFTY;miy=INFTY; loop(i,0,n){ cin>>x>>y; me=max(me,x+1); mix=min(x,mix); miy=min(y,miy); mh=max(mh,y+1); ps[i]=mp(x,y,i); } sort(ps,ps+n,sortByY); if(gen(0,me,mh)){ sort(ps,ps+n,sortByI); ps("TAK"); loop(i,0,n){ if(i==n-1) pln(ps[i].m); else ps(ps[i].m); } }else{ pln("NIE"); } //entr; } } |