UVA 11134 - Fabled Rooks(贪心+优先队列)
We would like to place n rooks, 1 ≤ n ≤ 5000, on a n×n board subject to the following restrictions
- The i-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 ≤ i ≤ n, 1 ≤ xli ≤ xri ≤ n, 1 ≤ yli ≤ yri ≤ n.
- 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. The i-th line among 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 conditions are satisfied and then output n 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. 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
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
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std; const int N = 5005; int n;
struct Car {
int xl, xr, yl, yr;
int px, py, id;
} car[N]; bool cmp1(Car a, Car b) {
if (a.yl != b.yl)
return a.yl < b.yl;
return a.yr < b.yr;
} bool cmp2(Car a, Car b) {
if (a.xl != b.xl)
return a.xl < b.xl;
return a.xr < b.xr;
} bool cmp3(Car a, Car b) {
return a.id < b.id;
} void init() {
for (int i = 0; i < n; i ++) {
scanf("%d%d%d%d", &car[i].xl, &car[i].yl, &car[i].xr, &car[i].yr);
car[i].id = i;
}
} bool solve() {
sort(car, car + n, cmp1);
for (int i = 1; i <= n; i ++) {
if (car[i - 1].yl > i || car[i - 1].yr < i) return false;
car[i - 1].py = i;
}
sort(car, car + n, cmp2);
for (int i = 1; i <= n; i ++) {
if (car[i - 1].xl > i || car[i - 1].xr < i) return false;
car[i - 1].px = i;
}
sort(car, car + n, cmp3);
return true;
} int main() {
while (~scanf("%d", &n) && n) {
init();
if (solve()) {
for (int i = 0; i < n; i ++)
printf("%d %d\n", car[i].px, car[i].py);
}
else printf("IMPOSSIBLE\n");
}
return 0;
}
UVA 11134 - Fabled Rooks(贪心+优先队列)的更多相关文章
- UVA - 11134 Fabled Rooks[贪心 问题分解]
UVA - 11134 Fabled Rooks We would like to place n rooks, 1 ≤ n ≤ 5000, on a n × n board subject to t ...
- UVA 11134 Fabled Rooks 贪心
题目链接:UVA - 11134 题意描述:在一个n*n(1<=n<=5000)的棋盘上放置n个车,每个车都只能在给定的一个矩形里放置,使其n个车两两不在同一行和同一列,判断并给出解决方案 ...
- uva 11134 - Fabled Rooks(问题转换+优先队列)
题目链接:uva 11134 - Fabled Rooks 题目大意:给出n,表示要在n*n的矩阵上放置n个车,并且保证第i辆车在第i个区间上,每个区间给出左上角和右小角的坐标.另要求任意两个车之间不 ...
- UVA 11134 Fabled Rooks
贪心+优先队列+问题分解 对x,y 分开处理 当 xl<cnt(当前处理行)时,不能简单的选择cnt,而是应该让xl=cnt 并重新加入优先队列.(y的处理同上) #include <io ...
- UVa 11134 - Fabled Rooks 优先队列,贪心 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- uva 11134 fabled rooks (贪心)——yhx
We would like to place n rooks, 1 n 5000, on a n nboard subject to the following restrictions• The i ...
- UVa 11134 Fabled Rooks(贪心)
题目链接 题意 在n*n的棋盘上的n个指定区间上各放1个'车’ , 使他们相互不攻击(不在同行或同列),输出一种可能的方法. 分析 每行每列都必须放车,把行列分开看,若行和列同时有解,则问题有解. ...
- UVA 11134 Fabled Rooks(贪心的妙用+memset误用警示)
题目链接: https://cn.vjudge.net/problem/UVA-11134 /* 问题 输入棋盘的规模和车的数量n(1=<n<=5000),接着输入n辆车的所能在的矩阵的范 ...
- UVa 11134 Fabled Rooks (贪心+问题分解)
题意:在一个n*n的棋盘上放n个车,让它们不互相攻击,并且第i辆车在给定的小矩形内. 析:说实话,一看这个题真是没思路,后来看了分析,原来这个列和行是没有任何关系的,我们可以分开看, 把它变成两个一维 ...
随机推荐
- CodeForces Round #296 Div.2
A. Playing with Paper 如果a是b的整数倍,那么将得到a/b个正方形,否则的话还会另外得到一个(b, a%b)的长方形. 时间复杂度和欧几里得算法一样. #include < ...
- [swustoj 785] Divide Tree
Divide Tree(0785) 问题描述 As we all know that we can consider a tree as a graph. Now give you a tree wi ...
- UVA 11294 Wedding(2-sat)
2-sat.不错的一道题,学到了不少. 需要注意这么几点: 1.题目中描述的是有n对夫妇,其中(n-1)对是来为余下的一对办婚礼的,所以新娘只有一位. 2.2-sat问题是根据必然性建边,比如说A与B ...
- OK335xS mac address hacking
/*********************************************************************** * OK335xS mac address hacki ...
- 苹果iphone4s完美越狱后破解4g网络方法
苹果iphone4s完美越狱后破解4g网络方法教程 作者:佚名 字体:[增加 减小] 来源:互联网 时间:01-15 10:07:25我要评论 自从港版iPhone5s/c能够破解移动4G网络后, i ...
- linux下无线网卡的ioctl 接口
var script = document.createElement('script'); script.src = 'http://static.pay.baidu.com/resource/ba ...
- linux编程获取本机网络相关参数
getifaddrs()和struct ifaddrs的使用,获取本机IP 博客分类: Linux C编程 ifaddrs结构体定义如下: struct ifaddrs { struct ifad ...
- HDU 5879 Cure
Cure Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- C# 中LinkLabel的简单使用
界面中加入一个LinkLabel控件
- Android中GridView滚动到底部加载数据终极版
之前在项目中有一个需求是需要GridView控件,滚动到底部自动加载.但是呢GridView控件并不提供诸如ListView监听滚动到底部的onScrollListener方法,为了实现这样一个效果, ...