UVA 11134 - Fabled Rooks

Fabled Rooks

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

The input consists of several test cases.The first line of each of them contains one integer number, n, theside of the board. n lines follow giving the rectangles wherethe rooks can be placed as described above. The i-th lineamong them gives xli, yli,xri, and yri. 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 conditionsare satisfied and then output n lines each giving the positionof a rook in order in which their rectangles appeared in the input. Ifthere are multiple solutions, any one will do. OutputIMPOSSIBLE 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

Output for sample input

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

K. Diks, adapted by P. Rudnicki


题目大意:nxn的棋盘上面放n个车,使得任意两个车不相互攻击,第i辆车在给定的矩形Ri中。


贪心,贪心思路和LA 4252(UVA 1422) Processor 差不多,行和列分开计算,不影响,

矩形表示为 行c[l,r] ,列r[l,r]

对与行,分别考虑第1行到第n行,对于当前行,选择上界小于当前行,并且下界超过当前行并且最小的 矩形。用了一个小技巧,从上到下考虑,则可以把上界小于当前行的矩形改成上界等于当前行,再入优先队列,就可以直接按下界贪心了。

同理可这样求列


#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <map>
#include <cmath>
#include <string>
#include <queue>
#include <set>

using namespace std;

#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#endif

#define S64I(a) scanf(iform, &a)
#define P64I(a) printf(oform, a)
const int INF = 0x3f3f3f3f;
const double eps = 10e-9;

const int maxn = 5000 + 50;

typedef struct SNode{
	int l, r;
	int id;
	bool operator < (const struct SNode & b) const {
		if(l == b.l) return r > b.r;
		return l > b.l;
	}
} Node;
Node r[maxn], c[maxn];
int ansr[maxn], ansc[maxn];

priority_queue<Node> qc, qr;

int main() {
	int n;

	while(scanf("%d", &n) != EOF && n) {
		while(!qc.empty()) qc.pop();
		while(!qr.empty()) qr.pop();
		for(int i=0; i<n; i++) {
			scanf("%d%d%d%d", &r[i].l, &c[i].l, &r[i].r, &c[i].r);
			r[i].id = c[i].id = i;
			qr.push(r[i]);
			qc.push(c[i]);
		}
		int flag = 1;
		int nowr = 1;
		while(!qr.empty() && nowr <= n) {
			Node it = qr.top();
			qr.pop();
			if(it.l > nowr) {
				flag = 0;
				break;
			}
			if(it.l < nowr) {
				it.l = nowr;
				qr.push(it);
				continue;
			}
			if(it.r < nowr) {
				continue;
			}
			ansr[it.id] = nowr++;
		}
		int nowc = 1;
		if(nowr > n && flag) {
			while(!qc.empty() && nowc <= n) {
				Node it = qc.top();
				qc.pop();
				if(it.l > nowc) {
					flag = 0;
					break;
				}
				if(it.l < nowc) {
					it.l = nowc;
					qc.push(it);
					continue;
				}
				if(it.r < nowc) {
					continue;
				}
				ansc[it.id] = nowc++;
			}
		}
		if(nowr > n && nowc > n && flag) {
			for(int i=0; i<n; i++) printf("%d %d\n", ansr[i], ansc[i]);
		} else {
			puts("IMPOSSIBLE");
		}
	}

	return 0;
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值