Original question:
We would like-to-place n rooks, 1≤n≤5000, in a NXN board subject to the following restrictions
the i-th rook can only is placed within the rectangle given by it left-upper Corner (XL I, yl i) and its right-lower C Orner (XR I, yr i), where 1≤i≤n, 1≤xl i≤xr i≤n, 1≤yl i≤yr i≤n.
• No-rooks can attack each of the other, which is No and both rooks can occupy the same column or the same row.
Input
The input consists of several test cases. The first line of each of them contains one integer number, n, the side of the board. N lines follow giving the rectangles where the rooks can be placed as described above. The i-th line among them gives XL I, yl i, XR I, and yr I. The input file is terminated with the integer ' 0 ' in a line by itself.
Output
Your task is to find such a placing of rooks, the above conditions is satisfied and then output n lines each giving T He position of a rook in order in which their rectangles appeared in the input. If There is multiple solutions, any one would do. Output ' Impossible ' if there is no such placing of the rooks.
Sample Input
8
1 1 2 2
5 7 8 8
2 2 5 5
2 2 5 5
6 3 8 6
6 3 8 5
6 3 8 8
3 6 7 8
8
1 1 2 2
5 7 8 8
2 2 5 5
2 2 5 5
6 3 8 6
6 3 8 5
6 3 8 8
3 6 7 8
0
Sample Output
1 1
5 8
2 4
4 2
7 3
8 5
6 6
3 7
1 1
5 8
2 4
4 2
7 3
8 5
6 6
3 7
English:
Give you a n*n chess board, and then give you the N group number, which represents the N rectangle box. The 4 dots in each group represent the upper-left and lower-right corners of the rectangle box, respectively. Now I'm going to ask you. The first point is placed in the I rectangle, and the row and column of the drop Point have no other points. Ask you how to put it.
#include <bits/stdc++.h> using namespace std;
struct point {int x,y,i;};
struct node {int x,i;};
int n;
Vector<point> vpx,vpy;
Vector<node> Ansx,ansy;
int CMP (const point &lhs,const point &rhs) {if (LHS.Y!=RHS.Y) return lhs.y<rhs.y;
else return lhs.x<rhs.x;
} int ncmp (const node &lhs,const node &RHS) {return lhs.i<rhs.i;} unordered_set<int> usi;
int main () {Ios::sync_with_stdio (false);
int flag;
while (cin>>n) {if (n==0) break;
flag=1;
Vpx.clear ();
Vpy.clear ();
Ansx.clear ();
Ansy.clear ();
for (int i=0;i<n;i++) {int xl,yl,xr,yr;
cin>>xl>>yl>>xr>>yr;
Vpx.push_back (Point{xl,xr,i});
Vpy.push_back (Point{yl,yr,i});
} sort (Vpx.begin (), Vpx.end (), CMP);
Sort (Vpy.begin (), Vpy.end (), CMP);
Usi.clear (); for (int i=0;i<vpx.size (); i++) {int mark=0;
for (int j=vpx[i].x;j<=vpx[i].y;j++) {if (Usi.find (j) ==usi.end ()) {
Ansx.push_back (node{j,vpx[i].i});
Usi.insert (j);
mark=1;
Break
}} if (!mark) {flag=0;
Break
}} if (flag) {usi.clear ();
for (int i=0;i<vpy.size (); i++) {int mark=0;
for (int j=vpy[i].x;j<=vpy[i].y;j++) {if (Usi.find (j) ==usi.end ())
{Ansy.push_back (node{j,vpy[i].i});
Usi.insert (j);
mark=1;
Break
}} if (!mark) {flag=0;
Break
}}} if (flag==0) {cout<< "impossible" <<endl;
} else {sort (Ansx.begin (), Ansx.end (), ncmp);
Sort (Ansy.begin (), Ansy.end (), ncmp); for (int i=0;i<ansx.size (); i++) {cout<<ansx[i].x<< "" <<ansy[i].x<<e
Ndl
}}} return 0;
}
idea:
Example of purple book
because there is no relationship between the vertical, so you can separate the rectangular interval into the X-band and the Y-interval to judge, and then use the greedy method, the interval according to the right endpoint value from small to large sorting, place points can be.
(cannot be sorted by left endpoint, e.g. [2,2] [1,3] three points)