Problem F: 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 (xliyli) and its right-lower corner (xriyri), 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 xliylixri, 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. 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 <iostream>
#include <cstdio>
#include <cstring>
//#include <cmath> 命名冲突 y1
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <cstdlib>
#include <sstream>
using namespace std;
typedef long long LL;
const int INF=0x5fffffff;
const double EXP=1e-;
const int MS=; int x1[MS], y1[MS], x2[MS], y2[MS], x[MS], y[MS]; /*
先将各个车分配在同一列的不同行,然后分配不同的列,
使他们彼此错开,任意两个车不在同一列和同一行。
也就是说行和列的分配时可以分开的。或者说独立的
使用贪心法分配。
*/ bool solve(int *a,int *b,int *c,int n)
{
// memset(c,-1,sizeof(c)); 注意这样是错误的,因为不知道c到哪里结束。字符串指针才可以,因为有结束符
fill(c,c+n,-); // ==-1表示还没有分配
for(int col=;col<=n;col++)
{
int rook=-,minb=n+;
for(int i=;i<n;i++)
{
if(c[i]<&&col>=a[i]&&b[i]<minb)
{
rook=i;
minb=b[i];
}
}
if(rook<||col>minb)
return false;
c[rook]=col;
}
return true;
} int main()
{
int n;
while(scanf("%d",&n)&&n)
{
for(int i=;i<n;i++)
scanf("%d%d%d%d",&x1[i],&y1[i],&x2[i],&y2[i]);
if(solve(x1,x2,x,n)&&solve(y1,y2,y,n))
for(int i=;i<n;i++)
printf("%d %d\n",x[i],y[i]);
else
printf("IMPOSSIBLE\n");
}
return ;
}

L - Fabled Rooks(中途相遇法和贪心)的更多相关文章

  1. UVA - 11134 Fabled Rooks问题分解,贪心

    题目:点击打开题目链接 思路:为了满足所有的车不能相互攻击,就要保证所有的车不同行不同列,于是可以发现,行与列是无关的,因此题目可以拆解为两个一维问题,即在区间[1-n]之间选择n个不同的整数,使得第 ...

  2. 01_传说中的车(Fabled Rooks UVa 11134 贪心问题)

    问题来源:刘汝佳<算法竞赛入门经典--训练指南> P81: 问题描述:你的任务是在n*n(1<=n<=5000)的棋盘上放n辆车,使得任意两辆车不相互攻击,且第i辆车在一个给定 ...

  3. 贪心 uvaoj 11134 Fabled Rooks

    Problem F: Fabled Rooks We would like to place n rooks, 1 ≤ n ≤ 5000, on a n×n board subject to the ...

  4. 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 ...

  5. uva 11134 - Fabled Rooks(问题转换+优先队列)

    题目链接:uva 11134 - Fabled Rooks 题目大意:给出n,表示要在n*n的矩阵上放置n个车,并且保证第i辆车在第i个区间上,每个区间给出左上角和右小角的坐标.另要求任意两个车之间不 ...

  6. uva 6757 Cup of Cowards(中途相遇法,貌似)

    uva 6757 Cup of CowardsCup of Cowards (CoC) is a role playing game that has 5 different characters (M ...

  7. LA 2965 Jurassic Remains (中途相遇法)

    Jurassic Remains Paleontologists in Siberia have recently found a number of fragments of Jurassic pe ...

  8. 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  9. 高效算法——J 中途相遇法,求和

    ---恢复内容开始--- J - 中途相遇法 Time Limit:9000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Su ...

随机推荐

  1. 使用cocos2d-x 3.2下载图片资源小例子

    cocos2d-x(ios)下载资源可以使用以下两种方式: 第一种使用libcurl下载图片 使用这种方法需要注意的是,我们需要引入libcurl.a这个库,同时配置对应的库目录和头文件目录具体方法是 ...

  2. Jquery添加移除样式

    获取与设置样式 获取class和设置class都可以使用attr()方法来完成.例如使用attr()方法来获取p元素的class,JQuery代码如下: var p_class = $("p ...

  3. HDU 2034 人见人爱A-B 分类: ACM 2015-06-23 23:42 9人阅读 评论(0) 收藏

    人见人爱A-B Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  4. USB枚举过程(1)

    总的过程 ① host检测到device,reset 获取设备描述符 host发获取设备描述符请求 ->setup ->data0 <-ack Divice 返回设备描述符 -> ...

  5. 完成端口iocp——在螺丝壳里做道场

    WINDOWS 2000以后的操作系统才支持IOCP.WINSOCK2.0才支持IOCP. 首先要有一个WINSOCK2.PAS的WINSOCK2.0接口调用声明单元. WINSOCK的版本号: WI ...

  6. ruby中实例变量、类变量等等的区别和联系

    ruby的变量有局部变量,全局变量,实例变量,类变量,常量. 1.局部变量 局部变量以一个小写字母开头或下划线开头 局部变量有局部作用域限制(比如一个block内),它的作用域起始于声明处,结束于该声 ...

  7. iOS开发中的测试框架

    转载作者:@crespoxiao 我们为什么要用测试框架呢?当然对项目开发有帮助了,但是业内现状是经常赶进度,所以TDD还是算了吧,BDD就测测数据存取和重要环节,这很重要,一次性跑完测试单元检查接口 ...

  8. ssh 框架整合试例 (spring+struts2+hibernate)

    1.首先用Eclipse创建一个web项目(Eclipse EE 版) new->Other-> 输入web 然后选择Dynamic Web Project->next-> 输 ...

  9. window.print打印指定div

    window.print可以打印网页,但有时候我们只希望打印特定控件或内容,怎么办呢? 首先我们可以把要打印的内容放在div中,然后用下面的代码进行打印. <html> <head& ...

  10. PHP 打印调用函数入口地址(堆栈),方便调式

    今天网站出现一个BUG,然后直接在数据库类里面写日志,看是哪条SQL出了问题,SQL语句到是找到了,但是不知道这条SQL语句来自何处,于是就想啊,如果能有一个办法,查看当前正在运行的这个方法是被哪个方 ...