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. 第二百零八天 how can I 坚持

    今天徐斌生日,生日快乐.买了两个小蛋糕,哈哈 还买了两条熊猫鱼.不知道鱼会不会冻死啊,买了加热器又不想用,看他们造化吧. LOL不错的游戏的. 睡觉,好冷.

  2. 老 base64 for xe8

    not recommend ,only for study procedure TForm1.Button3Click(Sender: TObject); var ssi, sso: TStringS ...

  3. Flex布局如何让子类在超出边界时隐藏掉

    在flex4中,因为必须添加<s:Scroller>标签才能出现滚动条,如果一个容器例如Panel没有添加滚动条,那么添加到Panel中的child的位置如果超出了Panel的边界,那么这 ...

  4. 测试peerdroid示例程序步骤

    来自JXTA交流群(36855950)...韦发改(992611244)  15:12:25—————————————————————————————————————————————————————— ...

  5. Oracle:递归查询(树形结构数据)

    今天要做一个查询功能:查询某用户所属部门,且包含该部门的所有上级部门信息.偶然找到了一个方法,特意来做个笔记.分享给和我一样的菜鸟,哈哈 查询子节点 1 select * 2 from d_arc_d ...

  6. Java浮点数精确计算

    BigDecimal是Java提供的一个不变的.任意精度的有符号十进制数对象.

  7. js 基于函数伪造的方式实现继承

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. Pgpool烂泥扶不上墙

    写这篇文章,是想好心地给打算使用Pgpool的人提个醒: Pgpool 真的不适合在企业范围使用. 我的主要理由是: 设计陈旧: 一旦后台任何节点Down掉,都会引发failover,它会杀掉所有子进 ...

  9. EasyUI-Tab 标签添加右键菜单

    在网上看了很多demo 自己实现了一个效果如下 <!doctype html> <html> <head> <meta http-equiv="co ...

  10. POJ 2376 Cleaning Shifts 贪心

    Cleaning Shifts 题目连接: http://poj.org/problem?id=2376 Description Farmer John is assigning some of hi ...