/********************************* ***** Common v2.0.8 ***** *********************************/ #include <bits/stdc++.h> #define ALL(container) std::begin(container), std::end(container) void test(); namespace common { template<typename T> T gcd(T const &a, T const &b) { return b == 0 ? a : gcd(b, a%b); } namespace io { template<typename T> inline std::ostream& operator<< (std::ostream& out, const std::vector<T>& data) { if(data.size() > 0) { out << data[0]; for( auto it = data.begin()+1; it != data.end(); it++ ) out << ' ' << *it; } return out; } template<typename T> inline std::istream& operator>> (std::istream& in, std::vector<T>& data) { for( auto &v : data ) in >> v; return in; } template<typename A, typename B> inline std::ostream& operator<< (std::ostream& out, const std::pair<A, B>& data) { return out << data.first << ' ' << data.second; } template<typename A, typename B> inline std::istream& operator>> (std::istream& in, std::pair<A, B>& data) { return in >> data.first >> data.second; } } template<typename T> class vector_from_one : public std::vector<T> { public: using std::vector<T>::vector; T& operator[] (size_t n) { return std::vector<T>::operator[](n-1); } const T& operator[] (size_t n) const { return std::vector<T>::operator[](n-1); } T& at (size_t n) { return std::vector<T>::at(n-1); } const T& at (size_t n) const { return std::vector<T>::at(n-1); } }; namespace functional { template<typename Type, typename C> struct GetComparator { using Comparator = C; }; template<typename Type> struct GetComparator<Type, void> { using Comparator = std::less<Type>; }; template<typename Object, typename Type, Type Object::* default_field, typename Comparator = void> struct CompareField { const Type Object::* field; CompareField() : field(default_field) {} CompareField(Type Object::* field) : field(field) {} constexpr bool operator()(Object const &lhs, Object const &rhs) const { return typename GetComparator<Type, Comparator>::Comparator()(lhs.*field, rhs.*field); } }; template<typename Comparator = void, typename Object, typename Type> const CompareField<Object, Type, nullptr, Comparator> compare_field(Type Object::* field) { return {field}; } template<typename iterator_type> class Iterable { typedef iterator_type iterator; const iterator begin_iterator; const iterator end_iterator; public: Iterable(iterator begin, iterator end) : begin_iterator(begin), end_iterator(end) {} iterator begin() { return begin_iterator; } iterator end() { return end_iterator; } }; template<typename iterator> Iterable<iterator> iterable(iterator begin, iterator end){ return Iterable<iterator>(begin, end); } template<typename Collection> auto reversed(Collection &collection) -> Iterable<decltype(collection.rbegin())> { return iterable(collection.rbegin(), collection.rend()); } } namespace operators { struct base_operator {}; template <typename LHS, typename OP> struct operator_proxy { LHS lhs; OP op; }; template <typename LHS, typename OP, typename SFINAE = typename std::enable_if<std::is_base_of<base_operator, typename std::remove_reference<OP>::type>::value>::type> operator_proxy<LHS, OP> operator<(LHS &&lhs, OP &&op) { return {lhs, op}; } template <typename LHS, typename OP, typename RHS> auto operator>(operator_proxy<LHS, OP> proxy, RHS &&rhs) -> decltype(proxy.op(proxy.lhs, rhs)) { proxy.op(proxy.lhs, rhs); } struct : public base_operator { template <typename T> void operator()(T &x, T y) const { x = std::max(x, y); } } const set_if_greater; struct : public base_operator { template <typename T> void operator()(T &x, T y) const { x = std::min(x, y); } } const set_if_less; } namespace main { int _default(int const, char const * const[]) { std::cout << "Undefined common::main app!" << std::endl; return 1; } int (*_app)(int const, char const * const[]); class one { static int main(int const argc, char const * const argv[]) { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); test(); return 0; } public: one() { _app = main; } }; class many { static int main(int const argc, char const * const argv[]) { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int T; std::cin >> T; while( T --> 0 ) test(); return 0; } public: many() { _app = main; } }; class all { static int main(int const argc, char const * const argv[]) { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cin.exceptions(std::ifstream::eofbit); try { while(std::cin) test(); } catch (std::ifstream::failure&) {} return 0; } public: all() { _app = main; } }; } } int main(int const argc, char const * const argv[]) { return common::main::_app(argc, argv); } using namespace std; using namespace common; using namespace common::io; using namespace common::functional; using namespace common::operators; // end of #include <common.hpp> //==================================================== main::many _; typedef map<int, map<int, int>> Corners; vector<int> answer; struct AddGap { int y; int x1, x2; bool operator< (AddGap const &rhs) const { return tie(y, x1) > tie(rhs.y, rhs.x1); } }; #if 0 #define debug(x) x #else #define debug(x) #endif bool solve(Corners const &corners, int width) { debug(cout << "solve(..., " << width << ")\n"); map<int, int> gaps; gaps[0] = width; priority_queue<AddGap> queue; for (auto &row : corners) { const int y = row.first; debug(cout << " y = " << y << "\n"); if (not queue.empty() and queue.top().y < y) { return false; } int last_gap_x1 = -1; int last_gap_x2 = -1; while (not queue.empty() and queue.top().y == y) { auto &gap = queue.top(); debug(cout << " gap = " << gap.x1 << " to " << gap.x2 << "\n"); if (gap.x1 != last_gap_x2) last_gap_x1 = gap.x1; last_gap_x2 = gaps[last_gap_x1] = gap.x2; queue.pop(); } for(auto it = begin(row.second); it != end(row.second);) { const int x = it->first; const int id = it->second; debug(cout << " Corner: (" << x << ", " << y << ")\n"); debug(cout << " has_gap = " << (gaps.empty() ? "no" : "yes") << "\n"); if (gaps.empty()) return false; auto gap = gaps.begin(); debug(cout << " gap = " << gap->first << " to " << gap->second <<"\n"); if (gap->first != x) return false; it = next(it); int limiting_x = (it != end(row.second) ? it->first : width); int gap_end = gap->second; int gap_size = gap_end - x; int size = min(limiting_x-x, gap_size); debug(cout << " size = " << size << "\n"); queue.push({y+size, x, x+size}); answer[id] = size; gaps.erase(gap); if (gap_size > size) { gaps[x+size] = gap_end; } } if (not gaps.empty()) return false; } int last_y = queue.top().y; while (not queue.empty()) { if (queue.top().y != last_y) return false; queue.pop(); } return true; } bool solve(Corners const &corners) { auto &first_row = corners.begin()->second; int last_x = first_row.rbegin()->first; int min_width = 0; for (auto &row : corners) min_width <set_if_greater> row.second.rbegin()->first + 1; for (auto &row : corners) { if (row.first == 0) continue; int width = row.first + last_x; if (width < min_width) continue; if (solve(corners, width)) return true; } return false; } bool trival_solve(int n) { if (n==1) cout << "TAK 1\n"; return n==1; } bool has_zero(Corners const &corners) { int x = corners.begin()->first; int y = corners.begin()->second.begin()->first; return x==0 and y==0; } void test() { Corners corners1; Corners corners2; int n; cin >> n; answer.resize(n); { vector<int> X(n), Y(n); for (int i = 0; i < n; ++i) cin >> X[i] >> Y[i]; int x0 = *min_element(ALL(X)); int y0 = *min_element(ALL(Y)); for (int i = 0; i < n; ++i) { int x = X[i] - x0; int y = Y[i] - y0; corners1[x][y] = i; corners2[y][x] = i; } } bool result = has_zero(corners1) and (trival_solve(n) or solve(corners1) or solve(corners2)); if (result) { cout << "TAK " << answer << "\n"; } else { cout << "NIE\n"; } }
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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | /********************************* ***** Common v2.0.8 ***** *********************************/ #include <bits/stdc++.h> #define ALL(container) std::begin(container), std::end(container) void test(); namespace common { template<typename T> T gcd(T const &a, T const &b) { return b == 0 ? a : gcd(b, a%b); } namespace io { template<typename T> inline std::ostream& operator<< (std::ostream& out, const std::vector<T>& data) { if(data.size() > 0) { out << data[0]; for( auto it = data.begin()+1; it != data.end(); it++ ) out << ' ' << *it; } return out; } template<typename T> inline std::istream& operator>> (std::istream& in, std::vector<T>& data) { for( auto &v : data ) in >> v; return in; } template<typename A, typename B> inline std::ostream& operator<< (std::ostream& out, const std::pair<A, B>& data) { return out << data.first << ' ' << data.second; } template<typename A, typename B> inline std::istream& operator>> (std::istream& in, std::pair<A, B>& data) { return in >> data.first >> data.second; } } template<typename T> class vector_from_one : public std::vector<T> { public: using std::vector<T>::vector; T& operator[] (size_t n) { return std::vector<T>::operator[](n-1); } const T& operator[] (size_t n) const { return std::vector<T>::operator[](n-1); } T& at (size_t n) { return std::vector<T>::at(n-1); } const T& at (size_t n) const { return std::vector<T>::at(n-1); } }; namespace functional { template<typename Type, typename C> struct GetComparator { using Comparator = C; }; template<typename Type> struct GetComparator<Type, void> { using Comparator = std::less<Type>; }; template<typename Object, typename Type, Type Object::* default_field, typename Comparator = void> struct CompareField { const Type Object::* field; CompareField() : field(default_field) {} CompareField(Type Object::* field) : field(field) {} constexpr bool operator()(Object const &lhs, Object const &rhs) const { return typename GetComparator<Type, Comparator>::Comparator()(lhs.*field, rhs.*field); } }; template<typename Comparator = void, typename Object, typename Type> const CompareField<Object, Type, nullptr, Comparator> compare_field(Type Object::* field) { return {field}; } template<typename iterator_type> class Iterable { typedef iterator_type iterator; const iterator begin_iterator; const iterator end_iterator; public: Iterable(iterator begin, iterator end) : begin_iterator(begin), end_iterator(end) {} iterator begin() { return begin_iterator; } iterator end() { return end_iterator; } }; template<typename iterator> Iterable<iterator> iterable(iterator begin, iterator end){ return Iterable<iterator>(begin, end); } template<typename Collection> auto reversed(Collection &collection) -> Iterable<decltype(collection.rbegin())> { return iterable(collection.rbegin(), collection.rend()); } } namespace operators { struct base_operator {}; template <typename LHS, typename OP> struct operator_proxy { LHS lhs; OP op; }; template <typename LHS, typename OP, typename SFINAE = typename std::enable_if<std::is_base_of<base_operator, typename std::remove_reference<OP>::type>::value>::type> operator_proxy<LHS, OP> operator<(LHS &&lhs, OP &&op) { return {lhs, op}; } template <typename LHS, typename OP, typename RHS> auto operator>(operator_proxy<LHS, OP> proxy, RHS &&rhs) -> decltype(proxy.op(proxy.lhs, rhs)) { proxy.op(proxy.lhs, rhs); } struct : public base_operator { template <typename T> void operator()(T &x, T y) const { x = std::max(x, y); } } const set_if_greater; struct : public base_operator { template <typename T> void operator()(T &x, T y) const { x = std::min(x, y); } } const set_if_less; } namespace main { int _default(int const, char const * const[]) { std::cout << "Undefined common::main app!" << std::endl; return 1; } int (*_app)(int const, char const * const[]); class one { static int main(int const argc, char const * const argv[]) { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); test(); return 0; } public: one() { _app = main; } }; class many { static int main(int const argc, char const * const argv[]) { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int T; std::cin >> T; while( T --> 0 ) test(); return 0; } public: many() { _app = main; } }; class all { static int main(int const argc, char const * const argv[]) { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cin.exceptions(std::ifstream::eofbit); try { while(std::cin) test(); } catch (std::ifstream::failure&) {} return 0; } public: all() { _app = main; } }; } } int main(int const argc, char const * const argv[]) { return common::main::_app(argc, argv); } using namespace std; using namespace common; using namespace common::io; using namespace common::functional; using namespace common::operators; // end of #include <common.hpp> //==================================================== main::many _; typedef map<int, map<int, int>> Corners; vector<int> answer; struct AddGap { int y; int x1, x2; bool operator< (AddGap const &rhs) const { return tie(y, x1) > tie(rhs.y, rhs.x1); } }; #if 0 #define debug(x) x #else #define debug(x) #endif bool solve(Corners const &corners, int width) { debug(cout << "solve(..., " << width << ")\n"); map<int, int> gaps; gaps[0] = width; priority_queue<AddGap> queue; for (auto &row : corners) { const int y = row.first; debug(cout << " y = " << y << "\n"); if (not queue.empty() and queue.top().y < y) { return false; } int last_gap_x1 = -1; int last_gap_x2 = -1; while (not queue.empty() and queue.top().y == y) { auto &gap = queue.top(); debug(cout << " gap = " << gap.x1 << " to " << gap.x2 << "\n"); if (gap.x1 != last_gap_x2) last_gap_x1 = gap.x1; last_gap_x2 = gaps[last_gap_x1] = gap.x2; queue.pop(); } for(auto it = begin(row.second); it != end(row.second);) { const int x = it->first; const int id = it->second; debug(cout << " Corner: (" << x << ", " << y << ")\n"); debug(cout << " has_gap = " << (gaps.empty() ? "no" : "yes") << "\n"); if (gaps.empty()) return false; auto gap = gaps.begin(); debug(cout << " gap = " << gap->first << " to " << gap->second <<"\n"); if (gap->first != x) return false; it = next(it); int limiting_x = (it != end(row.second) ? it->first : width); int gap_end = gap->second; int gap_size = gap_end - x; int size = min(limiting_x-x, gap_size); debug(cout << " size = " << size << "\n"); queue.push({y+size, x, x+size}); answer[id] = size; gaps.erase(gap); if (gap_size > size) { gaps[x+size] = gap_end; } } if (not gaps.empty()) return false; } int last_y = queue.top().y; while (not queue.empty()) { if (queue.top().y != last_y) return false; queue.pop(); } return true; } bool solve(Corners const &corners) { auto &first_row = corners.begin()->second; int last_x = first_row.rbegin()->first; int min_width = 0; for (auto &row : corners) min_width <set_if_greater> row.second.rbegin()->first + 1; for (auto &row : corners) { if (row.first == 0) continue; int width = row.first + last_x; if (width < min_width) continue; if (solve(corners, width)) return true; } return false; } bool trival_solve(int n) { if (n==1) cout << "TAK 1\n"; return n==1; } bool has_zero(Corners const &corners) { int x = corners.begin()->first; int y = corners.begin()->second.begin()->first; return x==0 and y==0; } void test() { Corners corners1; Corners corners2; int n; cin >> n; answer.resize(n); { vector<int> X(n), Y(n); for (int i = 0; i < n; ++i) cin >> X[i] >> Y[i]; int x0 = *min_element(ALL(X)); int y0 = *min_element(ALL(Y)); for (int i = 0; i < n; ++i) { int x = X[i] - x0; int y = Y[i] - y0; corners1[x][y] = i; corners2[y][x] = i; } } bool result = has_zero(corners1) and (trival_solve(n) or solve(corners1) or solve(corners2)); if (result) { cout << "TAK " << answer << "\n"; } else { cout << "NIE\n"; } } |