#include <iostream> #include <algorithm> #include <vector> #include <list> using namespace std; const int INFTY = 2000000000; struct point_t { point_t(int x, int y, int id) { this->x = x; this->y = y; this->id = id; this->square_side = 0; this->real = true; } int x; int y; int id; int square_side; bool real; }; bool cmp_by_x_y(const point_t &p1, const point_t &p2) { if (p1.x == p2.x) { return p1.y <= p2.y; } return p1.x < p2.x; } bool cmp_by_first(const pair<int, int> &p1, const pair<int, int> &p2) { return p1.first < p2.first; } bool cmp_by_id(const point_t &p1, const point_t &p2) { return p1.id < p2.id; } int get_largest_possible_square_side(list<point_t> &points, list<point_t>::iterator it) { point_t point = *it; int square_size = INFTY; list<point_t>::iterator itt = it; itt++; while (itt != points.end() && itt->x - point.x < square_size) { if (itt->y >= point.y) { int max_dist = max(itt->x - point.x, itt->y - point.y); if (max_dist < square_size) { square_size = max_dist; } } itt++; } point_t right_point(point.x + square_size, point.y, INFTY); right_point.square_side = -1; right_point.real = false; list<point_t>::iterator up = upper_bound(it, points.end(), right_point, cmp_by_x_y); if (up == points.end()) { points.push_back(right_point); } up--; if (up->x == right_point.x && up->y == right_point.y) { points.insert(up, right_point); } return square_size < INFTY ? square_size : 0; } int fill_square_sides(list<point_t> &points) { list<point_t>::iterator it = points.begin(); point_t leftmost_point = *it; int right_bound = leftmost_point.x; int upper_bound = leftmost_point.y; while (it != points.end()) { if (it->real) { point_t p = *it; if (p.y < leftmost_point.y) { // if there is such point then it is impossible to form a rectangle return 0; } it->square_side = get_largest_possible_square_side(points, it); right_bound = max(right_bound, it->x + it->square_side); upper_bound = max(upper_bound, it->y + it->square_side); } it++; } it = points.begin(); while (it != points.end()) { if (it->square_side == 0) { if (it->x == right_bound) { if (it->y != leftmost_point.y) { return 0; } it->square_side = upper_bound - leftmost_point.y; right_bound += it->square_side; } else if (it->y == upper_bound) { if (it->x != leftmost_point.x) { return 0; } it->square_side = right_bound - leftmost_point.x; upper_bound += it->square_side; } else { it->square_side = min(right_bound - it->x, upper_bound - it->y); } } it++; } return upper_bound - leftmost_point.y; } bool is_rectangle(vector<pair<int, int> > &v, int rectangle_height) { if (rectangle_height == 0) { return false; } int m = v.size(); int i = 0; int height = 0; while (i < m) { height += v[i].second; i++; while (i < m && v[i].first == v[i - 1].first) { height += v[i].second; i++; } if (i < m && height != rectangle_height) { return false; } } return true; } int main() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); list<point_t> points; for (int i = 0; i < n; i++) { int x, y; scanf("%d%d", &x, &y); points.push_back(point_t(x, y, i)); } points.sort(cmp_by_x_y); int rectangle_height = fill_square_sides(points); vector<pair<int, int> > v; list<point_t>::iterator it = points.begin(); while (it != points.end()) { if (it->real) { v.push_back(make_pair(it->x, it->square_side)); v.push_back(make_pair(it->x + it->square_side, -it->square_side)); } it++; } sort(v.begin(), v.end(), cmp_by_first); if (is_rectangle(v, rectangle_height)) { points.sort(cmp_by_id); printf("TAK"); for (it = points.begin(); it != points.end(); it++) { if (it->real) { printf(" %d", it->square_side); } } putchar('\n'); } else { puts("NIE"); } } 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 | #include <iostream> #include <algorithm> #include <vector> #include <list> using namespace std; const int INFTY = 2000000000; struct point_t { point_t(int x, int y, int id) { this->x = x; this->y = y; this->id = id; this->square_side = 0; this->real = true; } int x; int y; int id; int square_side; bool real; }; bool cmp_by_x_y(const point_t &p1, const point_t &p2) { if (p1.x == p2.x) { return p1.y <= p2.y; } return p1.x < p2.x; } bool cmp_by_first(const pair<int, int> &p1, const pair<int, int> &p2) { return p1.first < p2.first; } bool cmp_by_id(const point_t &p1, const point_t &p2) { return p1.id < p2.id; } int get_largest_possible_square_side(list<point_t> &points, list<point_t>::iterator it) { point_t point = *it; int square_size = INFTY; list<point_t>::iterator itt = it; itt++; while (itt != points.end() && itt->x - point.x < square_size) { if (itt->y >= point.y) { int max_dist = max(itt->x - point.x, itt->y - point.y); if (max_dist < square_size) { square_size = max_dist; } } itt++; } point_t right_point(point.x + square_size, point.y, INFTY); right_point.square_side = -1; right_point.real = false; list<point_t>::iterator up = upper_bound(it, points.end(), right_point, cmp_by_x_y); if (up == points.end()) { points.push_back(right_point); } up--; if (up->x == right_point.x && up->y == right_point.y) { points.insert(up, right_point); } return square_size < INFTY ? square_size : 0; } int fill_square_sides(list<point_t> &points) { list<point_t>::iterator it = points.begin(); point_t leftmost_point = *it; int right_bound = leftmost_point.x; int upper_bound = leftmost_point.y; while (it != points.end()) { if (it->real) { point_t p = *it; if (p.y < leftmost_point.y) { // if there is such point then it is impossible to form a rectangle return 0; } it->square_side = get_largest_possible_square_side(points, it); right_bound = max(right_bound, it->x + it->square_side); upper_bound = max(upper_bound, it->y + it->square_side); } it++; } it = points.begin(); while (it != points.end()) { if (it->square_side == 0) { if (it->x == right_bound) { if (it->y != leftmost_point.y) { return 0; } it->square_side = upper_bound - leftmost_point.y; right_bound += it->square_side; } else if (it->y == upper_bound) { if (it->x != leftmost_point.x) { return 0; } it->square_side = right_bound - leftmost_point.x; upper_bound += it->square_side; } else { it->square_side = min(right_bound - it->x, upper_bound - it->y); } } it++; } return upper_bound - leftmost_point.y; } bool is_rectangle(vector<pair<int, int> > &v, int rectangle_height) { if (rectangle_height == 0) { return false; } int m = v.size(); int i = 0; int height = 0; while (i < m) { height += v[i].second; i++; while (i < m && v[i].first == v[i - 1].first) { height += v[i].second; i++; } if (i < m && height != rectangle_height) { return false; } } return true; } int main() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); list<point_t> points; for (int i = 0; i < n; i++) { int x, y; scanf("%d%d", &x, &y); points.push_back(point_t(x, y, i)); } points.sort(cmp_by_x_y); int rectangle_height = fill_square_sides(points); vector<pair<int, int> > v; list<point_t>::iterator it = points.begin(); while (it != points.end()) { if (it->real) { v.push_back(make_pair(it->x, it->square_side)); v.push_back(make_pair(it->x + it->square_side, -it->square_side)); } it++; } sort(v.begin(), v.end(), cmp_by_first); if (is_rectangle(v, rectangle_height)) { points.sort(cmp_by_id); printf("TAK"); for (it = points.begin(); it != points.end(); it++) { if (it->real) { printf(" %d", it->square_side); } } putchar('\n'); } else { puts("NIE"); } } return 0; } |