#include <iostream> #include <vector> #include <algorithm> using namespace std; class Wierzcholek { public: Wierzcholek() { size = -1; } int x, y, nr; int size; bool operator<(const Wierzcholek &other) const { return x+y < other.x + other.y; } }; int negative() { cout << "NIE" << endl; return 0; } void kaskada(vector<Wierzcholek> &v, int i, int j) { for(int k = 0; k < i; k++) { if(v[k].size <= 0 && v[k].x >= v[j].x && v[k].x < v[j].x+v[j].size) { v[k].size = v[j].y - v[k].y; kaskada(v, i, k); } if(v[k].size <= 0 && v[k].y >= v[j].y && v[k].y < v[j].y+v[j].size) { v[k].size = v[j].x - v[k].x; kaskada(v, i, k); } } } int zrob_test() { int n; cin >> n; vector<Wierzcholek> v(n); for(int i = 0; i < n; i++) { v[i].nr = i; cin >> v[i].x >> v[i].y; } if(n == 1) { cout << "TAK 1" << endl; return 0; } sort(v.begin(), v.end()); for(int i = 1; i < v.size(); i++) { bool lewy = (v[i].x == v[0].x); bool dolny = (v[i].y == v[0].y); for(int j = 0; j < i; j++) { if(v[j].size > 0 && v[j].x + v[j].size > v[i].x && v[j].y + v[j].size > v[i].y) return negative(); if(v[j].size <= 0 && v[j].x < v[i].x && v[j].y < v[i].y) return negative(); if(v[j].size <= 0 && v[j].x == v[i].x) { v[j].size = v[i].y - v[j].y; kaskada(v, i, j); } if(v[j].size <= 0 && v[j].y == v[i].y) { v[j].size = v[i].x - v[j].x; kaskada(v, i, j); } if(v[j].x + v[j].size == v[i].x && v[j].y+v[j].size > v[i].y) lewy = true; if(v[j].y + v[j].size == v[i].y && v[j].x+v[j].size > v[i].x) dolny = true; } if(!lewy || !dolny) return negative(); } int maxx = -1, maxy = -1; for(int i = 0; i < v.size(); i++) { if (v[i].x == v[0].x) if(v[i].size > 0) maxy = v[i].y + v[i].size; else maxy = -1; if (v[i].y == v[0].y) if(v[i].size > 0) maxx = v[i].x + v[i].size; else maxx = -1; } if(maxx == -1) maxx = 2000000000; if(maxy == -1) maxy = 2000000000; for(int i = 0; i < v.size(); i++) { if(v[i].size > 0) continue; int distx = maxx - v[i].x; int disty = maxy - v[i].y; if(distx < disty) v[i].size = distx; else v[i].size = disty; } sort(v.begin(), v.end(), [](const Wierzcholek & a, const Wierzcholek & b) -> bool { return a.nr < b.nr; } ); //sortuje rosnaco po nrd for(int i = 0; i < v.size(); i++) cout << v[i].size << " "; cout << endl; } int main() { int t; cin >> t; for(int i =0; i < t; i++) zrob_test(); 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 | #include <iostream> #include <vector> #include <algorithm> using namespace std; class Wierzcholek { public: Wierzcholek() { size = -1; } int x, y, nr; int size; bool operator<(const Wierzcholek &other) const { return x+y < other.x + other.y; } }; int negative() { cout << "NIE" << endl; return 0; } void kaskada(vector<Wierzcholek> &v, int i, int j) { for(int k = 0; k < i; k++) { if(v[k].size <= 0 && v[k].x >= v[j].x && v[k].x < v[j].x+v[j].size) { v[k].size = v[j].y - v[k].y; kaskada(v, i, k); } if(v[k].size <= 0 && v[k].y >= v[j].y && v[k].y < v[j].y+v[j].size) { v[k].size = v[j].x - v[k].x; kaskada(v, i, k); } } } int zrob_test() { int n; cin >> n; vector<Wierzcholek> v(n); for(int i = 0; i < n; i++) { v[i].nr = i; cin >> v[i].x >> v[i].y; } if(n == 1) { cout << "TAK 1" << endl; return 0; } sort(v.begin(), v.end()); for(int i = 1; i < v.size(); i++) { bool lewy = (v[i].x == v[0].x); bool dolny = (v[i].y == v[0].y); for(int j = 0; j < i; j++) { if(v[j].size > 0 && v[j].x + v[j].size > v[i].x && v[j].y + v[j].size > v[i].y) return negative(); if(v[j].size <= 0 && v[j].x < v[i].x && v[j].y < v[i].y) return negative(); if(v[j].size <= 0 && v[j].x == v[i].x) { v[j].size = v[i].y - v[j].y; kaskada(v, i, j); } if(v[j].size <= 0 && v[j].y == v[i].y) { v[j].size = v[i].x - v[j].x; kaskada(v, i, j); } if(v[j].x + v[j].size == v[i].x && v[j].y+v[j].size > v[i].y) lewy = true; if(v[j].y + v[j].size == v[i].y && v[j].x+v[j].size > v[i].x) dolny = true; } if(!lewy || !dolny) return negative(); } int maxx = -1, maxy = -1; for(int i = 0; i < v.size(); i++) { if (v[i].x == v[0].x) if(v[i].size > 0) maxy = v[i].y + v[i].size; else maxy = -1; if (v[i].y == v[0].y) if(v[i].size > 0) maxx = v[i].x + v[i].size; else maxx = -1; } if(maxx == -1) maxx = 2000000000; if(maxy == -1) maxy = 2000000000; for(int i = 0; i < v.size(); i++) { if(v[i].size > 0) continue; int distx = maxx - v[i].x; int disty = maxy - v[i].y; if(distx < disty) v[i].size = distx; else v[i].size = disty; } sort(v.begin(), v.end(), [](const Wierzcholek & a, const Wierzcholek & b) -> bool { return a.nr < b.nr; } ); //sortuje rosnaco po nrd for(int i = 0; i < v.size(); i++) cout << v[i].size << " "; cout << endl; } int main() { int t; cin >> t; for(int i =0; i < t; i++) zrob_test(); return 0; } |