UVA 11134 Fabled Rooks 优先队列

来源:互联网 发布:网络教育系统下载 编辑:程序博客网 时间:2024/06/13 02:46

Problem F: Fabled Rooks

We would like to placen rooks, 1 ≤n ≤ 5000, on a n×nboard subject to the following restrictions
  • Thei-th rook can only be placed within the rectangle given by its left-upper corner (xli,yli) and its right-lower corner (xri,yri), where 1 ≤in, 1 ≤ xlixrin, 1 ≤ yliyrin.
  • No two rooks can attack each other, that is no two rooks can occupy the same column or the same row.

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. Thei-th line among them givesxli, yli,xri, andyri. The input file is terminated with the integer `0' on a line by itself.

Your task is to find such a placing of rooks that the above conditions are satisfied and then outputn lines each giving the position of a rook in order in which their rectangles appeared in the input. If there are multiple solutions, any one will do. OutputIMPOSSIBLE if there is no such placing of the rooks.

Sample input

81 1 2 25 7 8 82 2 5 52 2 5 56 3 8 66 3 8 56 3 8 83 6 7 881 1 2 25 7 8 82 2 5 52 2 5 56 3 8 66 3 8 56 3 8 83 6 7 80

Output for sample input

1 15 82 44 27 38 56 63 71 15 82 44 27 38 56 63 7



题意:有一个NxN的棋盘,你需要在上面放N个車,它们互相之间不能攻击到,并且每个車只能放在指定的矩形范围里面。


思路:乍一看好神....我们来思考一下题目要做的到底是什么。 首先車之间不能互相攻击,那么每行每列有且仅有一个車,我们把每个車用坐标(x,y)表示出来,那么最后要求的其实就是任意两个車的x坐标要不一样,任意两个車的y坐标不一样。然后每个車的x和y有自己的范围.....!!!x和y是相互独立的,不会相互影响!!所以我们只需要先求出各自的横坐标,然后再求出各自的纵坐标就行了,不是吗?这时候,问题就变得相当简单了。我们的题目变成了在一条X轴上,有n条线段,每条线段你必须取一个点,而且每个线段取的点要互不相同。这个我们不是做过吗?直接用优先队列贪心就行了。


代码:

#include <iostream>#include <cstdio>#include <cstring>#include <map>#include <cassert>#include <string>#include <queue>#include <cmath>#include <cmath>#include <algorithm>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define rep(i,a,b) for(int i=(a);i<(int)(b);++i)#define rrep(i,b,a) for(int i=(b);i>=(int)(a);--i)#define clr(a,x) memset(a,x,sizeof(a))#define mp make_pair#define eps 1e-10#define LL long long#define zero(x) (-eps < (x) && (x) < eps)const int maxn = 5000 + 5;int lx[maxn],rx[maxn],ly[maxn],ry[maxn];int X[maxn],Y[maxn];vector<int> v[maxn];int n;bool Try(int * l,int * r,int * ret){    rep(i,0,n) v[i].clear();    rep(i,0,n) v[l[i]].push_back(i);    priority_queue<pair<int,int> > q;    rep(i,0,n) {        rep(j,0,v[i].size()) {            int x = v[i][j];            q.push(mp(-r[x],x));        }        if (q.empty() || -q.top().first < i) return false;        int c = q.top().second; q.pop();        ret[c] = i;    }    return true;}bool solve(){    if (!Try(lx,rx,X) || !Try(ly,ry,Y)) return false;    rep(i,0,n) printf("%d %d\n",X[i]+1,Y[i]+1);    return true;}int main(){    #ifdef ACM        freopen("in.txt","r",stdin);    #endif // ACM    while (scanf("%d",&n),n) {        rep(i,0,n) {            scanf("%d%d%d%d",lx+i,ly+i,rx+i,ry+i);            --lx[i]; --ly[i]; --rx[i]; --ry[i];        }        if (!solve()) puts("IMPOSSIBLE");    }}


0 0
原创粉丝点击