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 (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
  
  对于一个N*N的棋盘,求放置N个有放置范围的车的一种方案,要求车不能相互攻击。
  这题有一个很巧妙的性质:行和列是不相关联的,考虑处理两次,贪心即可。
  
 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=; int L[maxn],R[maxn],U[maxn],D[maxn],P[maxn];
int X[maxn],Y[maxn];
bool cmp1(int a,int b){
if(R[a]!=R[b])return R[a]<R[b];
return L[a]<L[b];
} bool cmp2(int a,int b){
if(D[a]!=D[b])return D[a]<D[b];
return U[a]<U[b];
}
bool vis[maxn];
int main(){
int n,cnt;
while(~scanf("%d",&n)&&n){
for(int i=;i<=n;i++)
scanf("%d%d%d%d",&U[i],&L[i],&D[i],&R[i]);
for(int i=;i<=n;i++)P[i]=i;
bool OK=true,flag;
sort(P+,P+n+,cmp1);
memset(vis,,sizeof(vis));cnt=;
for(int i=,j;i<=n;i++){
while(vis[P[cnt]])cnt++;flag=false;
for(j=cnt;j<=n;j++)
if(!vis[P[j]]&&i<=R[P[j]]&&i>=L[P[j]]){
flag=true;
break;
}
OK&=flag;
if(!OK)break;
Y[P[j]]=i;
vis[P[j]]=true;
}
sort(P+,P+n+,cmp2);
memset(vis,,sizeof(vis));cnt=;
for(int i=,j;i<=n;i++){
while(vis[P[cnt]])cnt++;flag=false;
for(j=cnt;j<=n;j++)
if(!vis[P[j]]&&i<=D[P[j]]&&i>=U[P[j]]){
flag=true;
break;
}
OK&=flag;
if(!OK)break;
X[P[j]]=i;
vis[P[j]]=true;
}
if(OK){
for(int i=;i<=n;i++)
printf("%d %d\n",X[i],Y[i]);
}
else
printf("IMPOSSIBLE\n");
}
return ;
}
												

贪心 uvaoj 11134 Fabled Rooks的更多相关文章

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

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

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

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

  4. UVA 11134 - Fabled Rooks(贪心+优先队列)

    We would like to place  n  rooks, 1 ≤  n  ≤ 5000, on a  n×n  board subject to the following restrict ...

  5. UVa 11134 - Fabled Rooks 优先队列,贪心 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  6. UVa 11134 Fabled Rooks(贪心)

    题目链接  题意  在n*n的棋盘上的n个指定区间上各放1个'车’ , 使他们相互不攻击(不在同行或同列),输出一种可能的方法. 分析 每行每列都必须放车,把行列分开看,若行和列同时有解,则问题有解. ...

  7. UVA 11134 Fabled Rooks(贪心的妙用+memset误用警示)

    题目链接: https://cn.vjudge.net/problem/UVA-11134 /* 问题 输入棋盘的规模和车的数量n(1=<n<=5000),接着输入n辆车的所能在的矩阵的范 ...

  8. UVa 11134 Fabled Rooks (贪心+问题分解)

    题意:在一个n*n的棋盘上放n个车,让它们不互相攻击,并且第i辆车在给定的小矩形内. 析:说实话,一看这个题真是没思路,后来看了分析,原来这个列和行是没有任何关系的,我们可以分开看, 把它变成两个一维 ...

  9. UVA 11134 Fabled Rooks 贪心

    题目链接:UVA - 11134 题意描述:在一个n*n(1<=n<=5000)的棋盘上放置n个车,每个车都只能在给定的一个矩形里放置,使其n个车两两不在同一行和同一列,判断并给出解决方案 ...

随机推荐

  1. Bash函数使用

    #!/bin/bash function Fun_Name() { #function here echo "this is a function" } Fun_Name

  2. @ManyToMany中间表附加字段设计

    在使用@ManyToMany时,若中间表只有相应的外键字段可以直接建立两个对应的Entity 设置ManyToMany @ManyToMany 两个表多对多关联 但若是中间表有自己的附加字段,这需要为 ...

  3. maven 依赖冲突的问题

    如果原始系统的jarA依赖jarB1.0 而新引入的jarC依赖jarB2.0 导致系统启动报错,提示冲突,这时为了保证原始系统的稳定性, 在pom.xml文件中引入jarC时,需要exclude掉j ...

  4. Android之获取本地图片并压缩方法

    这两天在做项目时,做到上传图片功能一块时,碰到两个问题,一个是如何获取所选图片的路径,一个是如何压缩图片,在查了一些资料和看了别人写的后总算折腾出来了,在此记录一下. 首先既然要选择图片,我们就先要获 ...

  5. Web Service属性介绍

    每个 Web Service都需要唯一的命名空间,它可使客户端应用程序区分出可能使用相同方法名称的 Web Service.在 Visual Studio.NET中创建的Web Service的默认命 ...

  6. 表中查询重复的数据,如何通过sql语句查询?

    1.最直观的思路:要知道所有名字有重复人资料,首先必须知道哪个名字重复了:select name from emp group by name having count(*)>1所有名字重复人的 ...

  7. 八、C# 值类型

    结构.枚举.装箱.拆箱 自定义值类型 如何利用结构来定义新的值类型,并使之具有与大多数预定义 类型相似的行为,这里的关键在于,任何 新定义的值类型都有它们自己的数据和方法. 一般用枚举来定义常量值集合 ...

  8. Servlet(三)

    重定向 服务器向浏览器发送一个302状态码以及一个Location消息头(该消息头的值是一个地址,称之为重定向地址),浏览器收到后会立即向重定向的地址发出请求,使用相应对象的API方法实现(respo ...

  9. php 备份和还原数据库

    ignore_user_abort();//关掉浏览器,PHP脚本也可以继续执行. set_time_limit(0);// 通过set_time_limit(0)可以让程序无限制的执行下去 $int ...

  10. ARM架构下linux设备树加载的方法

    引入设备树后bootloader加载DTB方法: 1. 标准方法 将linux kernel放到内存地址为<kernel img addr>的内存中. 将DTB放到地址为<dtb a ...