Thinking about it:
The title is somewhat similar to the eight queens, but there is no limit in the direction of the diagonal, but a more restricted area. Because the problem of n max up to 5000, dare not rashly use backtracking. It can be learned that each rook in the title area is a rectangle, and each in the display, the x, Y axis is actually independent of each other, how to place the x-axis position does not affect the y-axis. So you can put the x-axis position, and then place the y-axis position, this way, the title looks like an interval-related greedy algorithm problem.
For x-axis interval, sort, rule: XR small row front, if XR same, then XL small row front. Select a small x whenever you choose the x position of an interval.
For the y-axis, this is the same way of handling.
Reference:
1.http://acm.lilingfei.com/uva-11134-fabled-rooks-%e4%be%8b%e9%a2%988-4_67
2. Algorithmic Competition Introduction Classic (second edition)
Ps:
At the beginning of the problem, the sorting ideas are wrong, the results are not found, until reference to other people's ideas (click here): if you press X as small as possible, sort must first by the XR small sort! Examples are:
The ranges of two zones A and B are [4,9] and [5,8] respectively. According to the collation, a should be ranked on the top of B:
(a) 4 5 6 7 8 9
(b) 5 6 7 8
If at this point 1-7 are already occupied by other vehicles, then a interval will be chosen to occupy 8. Interval B will return no solution. But actually a can account for 9, and B is 8.
Code:
/** * AC @ Sep 4th * Run time:0.009s */#include <bits/stdc++.h>using namespace std;struct Node {int XL, X R, YL, yr; int x, Y, order;};/ /Globalconst int MAXN = + 50;int n;std::vector<node> v;bool read () {cin >> N; if (N = = 0) {return false; } v.clear (); Node T; for (int i = 0; i < N; ++i) {cin >> t.xl >> t.yl >> t.xr >> t.yr; T.order = i; V.push_back (t); } return true; BOOL Cmp_x (node &a, node &b) {return A.XR < B.XR | | (A.XR = = B.XR && A.XL < B.XL);} BOOL Cmp_x (node &a, node &b) {//return A.XL < B.XL | | (A.XL = = B.xl && A.XR < B.XR);//}//This function was wrong used for sorting in this problem//CMP the R first, Then Lbool cmp_y (node &a, node &b) {return A.yr < B.yr | | (A.yr = = B.yr && a.yl < b.yl);} BOOL Cmp_order (node &a, node &b) {return a.order < B.order;} BOOL Is_possilE () {bool Vis[maxn] = {false}; Sort (V.begin (), V.end (), cmp_x); for (int i = 0; i < N; ++i) {int L = V[I].XL, r = V[I].XR; while (l <= R && Vis[l]) {++l; } if (L > R) {return false; } Vis[l] = true; v[i].x = l; } sort (V.begin (), V.end (), cmp_y); Memset (Vis, false, sizeof (VIS)); for (int i = 0; i < N; ++i) {int L = v[i].yl, r = v[i].yr; while (l <= R && Vis[l]) {++l; } if (L > R) {return false; } Vis[l] = true; V[I].Y = l; } return true; void output () {sort (V.begin (), V.end (), cmp_order); for (Std::vector<node>::iterator it = V.begin (); It! = V.end (); ++it) {cout << it->x << "" & lt;< it->y << Endl; }}void work () {if (is_possile () = = False) {cout << "impossible" << Endl; } else {output (); }}int Main (int argc, char const *argv[]) {Ios::sync_with_stdio (false); Cin.tie (0); while (read ()) {work (); } return 0;}
UVA 11134 fabled Rooks