Ultraviolet A 11134 Fabled Rooks

Source: Internet
Author: User

Place n cars on a n * n board so that they cannot attack each other (neither of them can be in the same row or column, for the I-th car, it can only be placed in a rectangle area (xli, yli). The vertices in the upper left corner of the rectangle are (xli, yli ), the vertex coordinates in the lower right corner are (xri, yri), 1 ≤ I ≤ n, 1 ≤ xli ≤ xri ≤ n, 1 ≤ yli ≤ yri ≤ n. train of Thought: in fact, the x-axis range and Y-axis range of each car area can be viewed independently. The areas of each car on the X-axis are [xl1, xr1], [xl2, xr2 ], [xl3, xr3 ]... on the Y axis, the region is [yl1, yr1], [yl2, yr2], [yl3, yr3 ]... if a piece is placed in each area on the X or Y axis without conflict, the condition is met. The judgment method is maintained by the priority queue. Each time the [l, r] interval is selected to be the smallest and the r interval is the smallest, we also need to maintain a number (maxx) that has been placed in the current directory. If l <maxx exists, modify this node, change its l to maxx, and then put it into the queue. [Cpp]/* ultraviolet A 11134 Fabled Rooks greedy, priority queue */# include <cstdio> # include <iostream> # include <algorithm> # include <queue> using namespace std; const int maxn = 5010; int n; struct Node {int l, r, id; friend bool operator <(const Node & a, const Node & B) {if (. l! = B. l) return. l> B. l; return. r> B. r ;}} arr1 [maxn], arr2 [maxn]; int ans [maxn] [2]; bool check (Node * arr, int pos) {priority_queue <Node> Q; for (int I = 0; I <n; ++ I) Q. push (arr [I]); int maxx = 0; while (! Q. empty () {Node tmp = Q. top (); Q. pop (); if (tmp. r <maxx) return false; if (tmp. l <maxx) {tmp. l = maxx; Q. push (tmp); continue;} int cur = max (maxx, tmp. l); ans [tmp. id] [pos] = cur; maxx = cur + 1;} return true;} int main () {int I, j; while (~ Scanf ("% d", & n) {for (I = 0; I <n; ++ I) {scanf ("% d", & arr1 [I]. l, & arr2 [I]. l, & arr1 [I]. r, & arr2 [I]. r); arr1 [I]. id = arr2 [I]. id = I;} if (check (arr1, 0) & check (arr2, 1) {for (I = 0; I <n; ++ I) printf ("% d \ n", ans [I] [0], ans [I] [1]);} else {puts ("IMPOSSIBLE ");}} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.