#include<stdio.h> #include<iostream> #include<limits.h> #include<algorithm> #include<set> using namespace std; struct point { long long x; long long y; }; inline bool operator<(const point& lhs, const point& rhs) { if(lhs.x == rhs.x){ return lhs.y < rhs.y; }else{ return lhs.x < rhs.x; } } struct solution { bool canBeSolved; long long* squares; }; long long POS_INF = LLONG_MAX; point findBottomLeft(int numberOfPoints, point* points){ set<point> pointsSet; long long minx = POS_INF; long long miny = POS_INF; point result; result.x = -1; result.y = -1; for(int i =0; i<numberOfPoints; ++i){ point p = points[i]; if(pointsSet.count(p)==0){ pointsSet.insert(p); }else{ return result; } minx = min(minx, p.x); miny = min(miny, p.y); } result.x = minx; result.y = miny; if(pointsSet.count(result)==1){ return result; }else{ result.x = -1; result.y = -1; return result; } } int indexOfFurthestRightBottom(int numberOfPoints, point* points, point bottomLeft){ long long searchedY = bottomLeft.y; long long maxX = bottomLeft.x; int index = 0; for(int i =0; i<numberOfPoints; ++i){ point p = points[i]; if(p.y == searchedY && p.x > maxX){ maxX = p.x; index = i; } } return index; } int indexOfFurthestTopLeft(int numberOfPoints, point* points, point bottomLeft){ long long searchedX = bottomLeft.x; long long maxY = bottomLeft.y; int index = 0; for(int i =0; i<numberOfPoints; ++i){ point p = points[i]; if(p.x == searchedX && p.y > maxY){ maxY = p.y; index = i; } } return index; } long long howMuchCanGrow(point p, int numberOfPoints, point* points, long long topWall, long long rightWall){ long long currentMax = POS_INF; for(int i =0; i<numberOfPoints; ++i) { point otherPoint = points[i]; if(otherPoint.x >= p.x && otherPoint.y >= p.y && !(p.x == otherPoint.x && p.y == otherPoint.y)){ long long xDiff = otherPoint.x - p.x; long long yDiff = otherPoint.y - p.y; long long constraint = max(xDiff, yDiff); currentMax = min(currentMax, constraint); } } if(topWall != POS_INF){ currentMax = min(currentMax, topWall - p.y); } if(rightWall != POS_INF){ currentMax = min(currentMax, rightWall - p.x); } return currentMax; } solution solve(int numberOfPoints, point* points){ solution s; s.squares = new long long[numberOfPoints]; s.canBeSolved = true; point bottomLeft = findBottomLeft(numberOfPoints, points); long long topWall = POS_INF; long long rightWall = POS_INF; if(bottomLeft.x < 0 || bottomLeft.y < 0){ s.canBeSolved=false; }else{ for(int i=0; i<numberOfPoints; ++i){ s.squares[i] = howMuchCanGrow(points[i], numberOfPoints, points, topWall, rightWall); } int topLeftIndex = indexOfFurthestTopLeft(numberOfPoints, points, bottomLeft); int rightBottomIndex = indexOfFurthestRightBottom(numberOfPoints, points, bottomLeft); if(s.squares[topLeftIndex]!=POS_INF){ topWall = points[topLeftIndex].y + s.squares[topLeftIndex]; } if(s.squares[rightBottomIndex]!=POS_INF){ rightWall= points[rightBottomIndex].x + s.squares[rightBottomIndex]; } for(int i=0; i<numberOfPoints; ++i){ s.squares[i] = howMuchCanGrow(points[i], numberOfPoints, points, topWall, rightWall); } } return s; } int main() { cin.sync_with_stdio(false); int tests; cin >> tests; for(int i =0; i<tests; ++i){ int numberOfPoints; cin >> numberOfPoints; point* points = new point[numberOfPoints]; for(int j=0; j<numberOfPoints; ++j){ cin >> points[j].x; cin >> points[j].y; } solution s = solve(numberOfPoints, points); if(!s.canBeSolved){ cout << "NIE" << endl; }else{ cout << "TAK"; for(int j=0; j<numberOfPoints; ++j){ cout << " " << s.squares[j]; } cout << endl; delete[] s.squares; } delete[] points; } 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 | #include<stdio.h> #include<iostream> #include<limits.h> #include<algorithm> #include<set> using namespace std; struct point { long long x; long long y; }; inline bool operator<(const point& lhs, const point& rhs) { if(lhs.x == rhs.x){ return lhs.y < rhs.y; }else{ return lhs.x < rhs.x; } } struct solution { bool canBeSolved; long long* squares; }; long long POS_INF = LLONG_MAX; point findBottomLeft(int numberOfPoints, point* points){ set<point> pointsSet; long long minx = POS_INF; long long miny = POS_INF; point result; result.x = -1; result.y = -1; for(int i =0; i<numberOfPoints; ++i){ point p = points[i]; if(pointsSet.count(p)==0){ pointsSet.insert(p); }else{ return result; } minx = min(minx, p.x); miny = min(miny, p.y); } result.x = minx; result.y = miny; if(pointsSet.count(result)==1){ return result; }else{ result.x = -1; result.y = -1; return result; } } int indexOfFurthestRightBottom(int numberOfPoints, point* points, point bottomLeft){ long long searchedY = bottomLeft.y; long long maxX = bottomLeft.x; int index = 0; for(int i =0; i<numberOfPoints; ++i){ point p = points[i]; if(p.y == searchedY && p.x > maxX){ maxX = p.x; index = i; } } return index; } int indexOfFurthestTopLeft(int numberOfPoints, point* points, point bottomLeft){ long long searchedX = bottomLeft.x; long long maxY = bottomLeft.y; int index = 0; for(int i =0; i<numberOfPoints; ++i){ point p = points[i]; if(p.x == searchedX && p.y > maxY){ maxY = p.y; index = i; } } return index; } long long howMuchCanGrow(point p, int numberOfPoints, point* points, long long topWall, long long rightWall){ long long currentMax = POS_INF; for(int i =0; i<numberOfPoints; ++i) { point otherPoint = points[i]; if(otherPoint.x >= p.x && otherPoint.y >= p.y && !(p.x == otherPoint.x && p.y == otherPoint.y)){ long long xDiff = otherPoint.x - p.x; long long yDiff = otherPoint.y - p.y; long long constraint = max(xDiff, yDiff); currentMax = min(currentMax, constraint); } } if(topWall != POS_INF){ currentMax = min(currentMax, topWall - p.y); } if(rightWall != POS_INF){ currentMax = min(currentMax, rightWall - p.x); } return currentMax; } solution solve(int numberOfPoints, point* points){ solution s; s.squares = new long long[numberOfPoints]; s.canBeSolved = true; point bottomLeft = findBottomLeft(numberOfPoints, points); long long topWall = POS_INF; long long rightWall = POS_INF; if(bottomLeft.x < 0 || bottomLeft.y < 0){ s.canBeSolved=false; }else{ for(int i=0; i<numberOfPoints; ++i){ s.squares[i] = howMuchCanGrow(points[i], numberOfPoints, points, topWall, rightWall); } int topLeftIndex = indexOfFurthestTopLeft(numberOfPoints, points, bottomLeft); int rightBottomIndex = indexOfFurthestRightBottom(numberOfPoints, points, bottomLeft); if(s.squares[topLeftIndex]!=POS_INF){ topWall = points[topLeftIndex].y + s.squares[topLeftIndex]; } if(s.squares[rightBottomIndex]!=POS_INF){ rightWall= points[rightBottomIndex].x + s.squares[rightBottomIndex]; } for(int i=0; i<numberOfPoints; ++i){ s.squares[i] = howMuchCanGrow(points[i], numberOfPoints, points, topWall, rightWall); } } return s; } int main() { cin.sync_with_stdio(false); int tests; cin >> tests; for(int i =0; i<tests; ++i){ int numberOfPoints; cin >> numberOfPoints; point* points = new point[numberOfPoints]; for(int j=0; j<numberOfPoints; ++j){ cin >> points[j].x; cin >> points[j].y; } solution s = solve(numberOfPoints, points); if(!s.canBeSolved){ cout << "NIE" << endl; }else{ cout << "TAK"; for(int j=0; j<numberOfPoints; ++j){ cout << " " << s.squares[j]; } cout << endl; delete[] s.squares; } delete[] points; } return 0; } |