Fabled Rooks

题目:

在给定区域内放车,使得所有车在不同行且不同列(无法互相攻击)

思路:


这个和processor很像,都是在给定范围内安排事件,所以都用到了优先队列,按照前端点优先.

这里要提到由于水平和垂直的位置确定不影响,所以分开确定,即一个车的位置确定分两次.

同时用m来控制下个元素的最小放置位置,如果某元素的最远范围小于m显然是不行的。


#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 5005;
int n;
int ans[maxn][2];
struct pos{  
	int l, r, id;  
	friend bool operator<(const pos& a, const pos&b){  
		if(a.l != b.l) return a.l > b.l;  
		return a.r > b.r;  
	}  
}arr1[maxn], arr2[maxn];  
bool check(pos* arr, int p) {
	priority_queue<pos> q;
	for(int i=0; i<n; i++) q.push(arr[i]);//所有元素入队。
	int m = 0;
	while(!q.empty()) {
		pos tmp = q.top();
		q.pop();
		if(tmp.r < m) return false;
		if(tmp.l < m) {
			tmp.l = m;
			q.push(tmp);
			continue;
		}
		int cnt = max(m, tmp.l);
		ans[tmp.id][p] = cnt;
		m = cnt+1;
	}
	return true;
}
int main() {
	while(scanf("%d", &n) != EOF && n) {
		for(int i=0; i<n; i++) {
			scanf("%d%d%d%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(int i=0; i<n; i++) printf("%d %d\n", ans[i][0], ans[i][1]);
		}
		else printf("IMPOSSIBLE\n");
	}
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值