#include <iostream> #include <assert.h> #include <algorithm> #include <list> #include <vector> using namespace std; #define MAXN 2000 #define POINT_MAX 2000000000 #define POINT_MIN 0 #define NIE {\ cout << "NIE" << endl;\ return;\ } #define TAK {\ printSides();\ return;\ } struct Point { int x, y; bool operator!=(const Point &rhs) const { return (*this).x != rhs.x || (*this).y != rhs.y; } }; struct Square { // Square(Point point, int side, bool border) { // this->point = point; // } Point point; int side; bool border; }; vector<Square> squares; struct ComparePoints{ bool operator() (const Point &lhs, const Point &rhs) { return lhs.x<=rhs.x && lhs.y<=rhs.y && lhs != rhs; } }comparePoints; void loadPoints(int n); bool next_repetition_permutation(vector<int>::iterator iterator, vector<int>::iterator normal_iterator); void updateBorders(const vector<int> &borderIndices, const vector<int> &repetitionPermutation); void debugWhile(const vector<int> &vector); int distance(Point l, Point r) { return max(r.x-l.x, r.y-l.y); } int t, n; Point points[MAXN]; //https://stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other bool overlap(const Square &s1, const Square &s2) { return s1.point.x < s2.point.x + s2.side && s1.point.x + s1.side > s2.point.x && s1.point.y < s2.point.y + s2.side && s1.point.y + s1.side > s2.point.y; } bool checkNoOverlap() { for (size_t i = 0; i < squares.size()-1; i++) { for (size_t j = i+1; j < squares.size(); j++) { if(overlap(squares[i], squares[j])) return false; } } return true; } Square findNearest(Point from) { int minDistance = POINT_MAX; bool found = false; for (size_t i = 0; i < n; i++) { if(comparePoints(from, points[i])) { const int d = distance(from, points[i]); if(d < minDistance) { minDistance = d; found = true; } } } // cout << "find nearest: " << minDistance << endl; if(!found) return Square{{from.x, from.y}, 1, true}; return Square{from, minDistance, false}; } long long areaOfContainingRectangle(Point origin, Square topSquare, Square rightSquare) { const long long eastNess = rightSquare.point.x + rightSquare.side; const long long northNess = topSquare.point.y + topSquare.side; const long long width = eastNess - origin.x; const long long height = northNess - origin.y; return width * height; } long long calculateAreaOfContainingRectangle() { Point origin{ POINT_MAX, POINT_MAX}; Square right{Point{POINT_MIN, POINT_MIN}, 0}, top{Point{POINT_MIN, POINT_MIN}, 0}; for (size_t i = 0; i < n; i++) { Point point = points[i]; if(origin.x >= point.x) { origin.x = point.x; } if(origin.y >= point.y) { origin.y = point.y; } } for(auto square : squares) { if (right.point.x <= square.point.x) { right = square; } if (top.point.y <= square.point.y) { top = square; } } return areaOfContainingRectangle(origin, top, right); } long long sumOfSquareAreas() { long long result = 0; for(auto square : squares) { result += square.side * square.side; } return result; } void printSides() { cout << "TAK" << " "; for(auto square : squares) { cout << square.side << " "; } cout << endl; } void solveProblem(int n) { loadPoints(n); squares.clear(); for (size_t i = 0; i < n; i++) { Square corner = findNearest(points[i]); squares.push_back(corner); } vector<int> borderIndices; vector<int> repetitionPermutation; for(int i = 0; i < squares.size(); i++) { if(squares[i].border) { borderIndices.push_back(i); repetitionPermutation.push_back(1); } } if(borderIndices.size() == 0) { updateBorders(borderIndices, repetitionPermutation); if(!checkNoOverlap()) NIE long long containing = calculateAreaOfContainingRectangle(); long long sum = sumOfSquareAreas(); if(containing != sum) NIE TAK } do { updateBorders(borderIndices, repetitionPermutation); if(!checkNoOverlap()) continue; long long containing = calculateAreaOfContainingRectangle(); long long sum = sumOfSquareAreas(); if(containing != sum) continue; TAK }while(next_repetition_permutation(repetitionPermutation.begin(), repetitionPermutation.end())); NIE } void updateBorders(const vector<int> &borderIndices, const vector<int> &repetitionPermutation) { for (int i = 0; i < borderIndices.size(); ++i) { squares[borderIndices[i]].side = repetitionPermutation[i]; } } bool next_repetition_permutation(vector<int>::iterator iterator, vector<int>::iterator end) { const int min = 1; const int max = 5; bool last = true; do { if(*iterator == max) *iterator = min; else { (*iterator)++; last = false; break; } }while (iterator++ != end); return !last; } void loadPoints(int n) { for (size_t i = 0; i < n; i++) { cin >> points[i].x; cin >> points[i].y; } } int main() { ios_base::sync_with_stdio(false); cin >> t; for (size_t i = 0; i < t; i++) { cin >> n; solveProblem(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 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 | #include <iostream> #include <assert.h> #include <algorithm> #include <list> #include <vector> using namespace std; #define MAXN 2000 #define POINT_MAX 2000000000 #define POINT_MIN 0 #define NIE {\ cout << "NIE" << endl;\ return;\ } #define TAK {\ printSides();\ return;\ } struct Point { int x, y; bool operator!=(const Point &rhs) const { return (*this).x != rhs.x || (*this).y != rhs.y; } }; struct Square { // Square(Point point, int side, bool border) { // this->point = point; // } Point point; int side; bool border; }; vector<Square> squares; struct ComparePoints{ bool operator() (const Point &lhs, const Point &rhs) { return lhs.x<=rhs.x && lhs.y<=rhs.y && lhs != rhs; } }comparePoints; void loadPoints(int n); bool next_repetition_permutation(vector<int>::iterator iterator, vector<int>::iterator normal_iterator); void updateBorders(const vector<int> &borderIndices, const vector<int> &repetitionPermutation); void debugWhile(const vector<int> &vector); int distance(Point l, Point r) { return max(r.x-l.x, r.y-l.y); } int t, n; Point points[MAXN]; //https://stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other bool overlap(const Square &s1, const Square &s2) { return s1.point.x < s2.point.x + s2.side && s1.point.x + s1.side > s2.point.x && s1.point.y < s2.point.y + s2.side && s1.point.y + s1.side > s2.point.y; } bool checkNoOverlap() { for (size_t i = 0; i < squares.size()-1; i++) { for (size_t j = i+1; j < squares.size(); j++) { if(overlap(squares[i], squares[j])) return false; } } return true; } Square findNearest(Point from) { int minDistance = POINT_MAX; bool found = false; for (size_t i = 0; i < n; i++) { if(comparePoints(from, points[i])) { const int d = distance(from, points[i]); if(d < minDistance) { minDistance = d; found = true; } } } // cout << "find nearest: " << minDistance << endl; if(!found) return Square{{from.x, from.y}, 1, true}; return Square{from, minDistance, false}; } long long areaOfContainingRectangle(Point origin, Square topSquare, Square rightSquare) { const long long eastNess = rightSquare.point.x + rightSquare.side; const long long northNess = topSquare.point.y + topSquare.side; const long long width = eastNess - origin.x; const long long height = northNess - origin.y; return width * height; } long long calculateAreaOfContainingRectangle() { Point origin{ POINT_MAX, POINT_MAX}; Square right{Point{POINT_MIN, POINT_MIN}, 0}, top{Point{POINT_MIN, POINT_MIN}, 0}; for (size_t i = 0; i < n; i++) { Point point = points[i]; if(origin.x >= point.x) { origin.x = point.x; } if(origin.y >= point.y) { origin.y = point.y; } } for(auto square : squares) { if (right.point.x <= square.point.x) { right = square; } if (top.point.y <= square.point.y) { top = square; } } return areaOfContainingRectangle(origin, top, right); } long long sumOfSquareAreas() { long long result = 0; for(auto square : squares) { result += square.side * square.side; } return result; } void printSides() { cout << "TAK" << " "; for(auto square : squares) { cout << square.side << " "; } cout << endl; } void solveProblem(int n) { loadPoints(n); squares.clear(); for (size_t i = 0; i < n; i++) { Square corner = findNearest(points[i]); squares.push_back(corner); } vector<int> borderIndices; vector<int> repetitionPermutation; for(int i = 0; i < squares.size(); i++) { if(squares[i].border) { borderIndices.push_back(i); repetitionPermutation.push_back(1); } } if(borderIndices.size() == 0) { updateBorders(borderIndices, repetitionPermutation); if(!checkNoOverlap()) NIE long long containing = calculateAreaOfContainingRectangle(); long long sum = sumOfSquareAreas(); if(containing != sum) NIE TAK } do { updateBorders(borderIndices, repetitionPermutation); if(!checkNoOverlap()) continue; long long containing = calculateAreaOfContainingRectangle(); long long sum = sumOfSquareAreas(); if(containing != sum) continue; TAK }while(next_repetition_permutation(repetitionPermutation.begin(), repetitionPermutation.end())); NIE } void updateBorders(const vector<int> &borderIndices, const vector<int> &repetitionPermutation) { for (int i = 0; i < borderIndices.size(); ++i) { squares[borderIndices[i]].side = repetitionPermutation[i]; } } bool next_repetition_permutation(vector<int>::iterator iterator, vector<int>::iterator end) { const int min = 1; const int max = 5; bool last = true; do { if(*iterator == max) *iterator = min; else { (*iterator)++; last = false; break; } }while (iterator++ != end); return !last; } void loadPoints(int n) { for (size_t i = 0; i < n; i++) { cin >> points[i].x; cin >> points[i].y; } } int main() { ios_base::sync_with_stdio(false); cin >> t; for (size_t i = 0; i < t; i++) { cin >> n; solveProblem(n); } return 0; } |