Problem UVA11134-Fabled Rooks

Accept: 716  Submit: 6134
Time Limit: 3000mSec

Problem Description

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

Input

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.

 Output

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

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
 
题解:有了lrj前面几道关于区间的例题,这个题的贪心还是很好想的,首先因为行列独立,因此可以转化为两个问题分析,在区间1~n之间选n个互不相同的数,使得第i个数在区间[ai,bi].区间排序时按右端点递增排,右端点相同时按左端点从大到小排。每次选取区间最左边能选的点。当有一个区间不能选时就是IMPOSSIBLE.我粗略地证明一下:假设在当前区间不选可选的最左边的点,必定选取了一个更靠右的点,最左边的点肯定也是要填的,之后填这个格子的区间如果是左端点小于等于当前区间的区间,那么交换填的方式不影响可解性,如果左端点大于当前区间左端点,那么当前区间尽量向左填所得到的解不会比当前解差。有不对的地方,欢迎大家指教。
 
 #include <bits/stdc++.h>

 using namespace std;

 const int maxn =  + ;

 int n;

 struct Interval {
pair<int, int> interval;
int pos;
}hor[maxn], ver[maxn]; bool cmp(const Interval &a, const Interval &b) {
if (a.interval.second == b.interval.second) return a.interval.first > b.interval.first;
else return a.interval.second < b.interval.second;
} bool hvis[maxn], vvis[maxn];
int vans[maxn], hans[maxn]; int main()
{
//freopen("input.txt", "r", stdin);
while (~scanf("%d", &n) && n) {
memset(hvis, false, sizeof(hvis));
memset(vvis, false, sizeof(vvis));
int xl, yl, xr, yr;
for (int i = ; i <= n; i++) {
scanf("%d%d%d%d", &xl, &yl, &xr, &yr);
ver[i].interval = make_pair(xl, xr);
ver[i].pos = i;
hor[i].interval = make_pair(yl, yr);
hor[i].pos = i;
} sort(ver + , ver + + n, cmp);
sort(hor + , hor + + n, cmp); bool ok = true; for (int i = ; i <= n; i++) {
int l = ver[i].interval.first, r = ver[i].interval.second;
int p = ver[i].pos;
int j;
for (j = l; j <= r; j++) {
if (!vvis[j]) {
vvis[j] = true;
vans[p] = j;
break;
}
}
if (j == r + ) { ok = false; break; }
} if (!ok) {
printf("IMPOSSIBLE\n");
continue;
} for (int i = ; i <= n; i++) {
int l = hor[i].interval.first, r = hor[i].interval.second;
int p = hor[i].pos;
int j;
for (j = l; j <= r; j++) {
if (!hvis[j]) {
hvis[j] = true;
hans[p] = j;
break;
}
}
if (j == r + ) { ok = false; break; }
} if (!ok) {
printf("IMPOSSIBLE\n");
continue;
} for (int i = ; i <= n; i++) {
printf("%d %d\n", vans[i], hans[i]);
}
}
return ;
}
 

 Sample Output

Problem UVA11134-Fabled Rooks(贪心)的更多相关文章

  1. UVA-11134 Fabled Rooks 贪心问题(区间贪心)

    题目链接:https://cn.vjudge.net/problem/UVA-11134 题意 在 n*n 的棋盘上,放上 n 个车(ju).使得这 n 个车互相不攻击,即任意两个车不在同一行.同一列 ...

  2. uva11134 - Fabled Rooks(问题分解,贪心法)

    这道题非常好,不仅用到了把复杂问题分解为若干个熟悉的简单问题的方法,更是考察了对贪心法的理解和运用是否到位. 首先,如果直接在二维的棋盘上考虑怎么放不好弄,那么注意到x和y无关(因为两个车完全可以在同 ...

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

  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. Uva11134 Fabled Rooks

    普通的贪心题. 虽然图是二维的,但可以把横向和纵向分开处理. 将区间按右端点排序,然后从区间左端点到右端点找第一个空位置放棋子即可. /*by SilverN*/ #include<algori ...

  6. UVA 11134 Fabled Rooks 贪心

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

  7. L - Fabled Rooks(中途相遇法和贪心)

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

  8. 贪心 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 ...

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

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

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

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

随机推荐

  1. 8;XHTML 框架

    1.多窗框的基本结构 2.嵌套多窗体设置 3.多框架与超链接 4.悬浮窗体的设置 浏览器视窗本身就是一个框架,网页就是显示在该单 一的框架内,本章将介绍另一种网页呈现的方式,那就是可将原先单一的框架分 ...

  2. 微信wx.request

    官方 wx.request 代码,Post 没成功过,使用Get 方式成功了. wx.request({ url: 'test.php', //仅为示例,并非真实的接口地址 data: { x: '' ...

  3. input file图片上传

    <div class="div-title"> <h5>图片上传</h5> <div class="photo-box" ...

  4. TFS 2017 持续集成速记

    VS2017许多激动人 心的功能,升级!   TFS2017也升级,不支持SQL2012,升级!不过貌似开发版不能升级,好吧,开发版升2014企业版! 2017据说不支持XAML生成了,但后台菜单中还 ...

  5. BDD实战篇 - 在.NET Core下安装Specflow

    这是<如何用ABP框架快速完成项目 >系列中的一篇文章. BDD很赞!比TDD先进很多,能够大大提高编码效率. 让我们动手起来吧!先在.NET Core下安装Specflow! 官网教程在 ...

  6. Ubuntu、deepin 支持 yum

    1,首先检测是否安装了build-essential程序包 sudo apt-get install build-essential 2,安装 yum sudo apt-get yum 3,检测是否安 ...

  7. concrrent类下 BlockingDeque 下 自己实现代码编写

    一.LinkedBlockingDeque简介 java6增加了两种容器类型,Deque和BlockingDeque,它们分别对Queue和BlockingQueue进行了扩展. Deque是一个双端 ...

  8. Kotlin入门(24)如何自定义视图

    Android提供了丰富多彩的视图与控件,已经能够满足大部分的业务需求,然而计划赶不上变化,总是有意料之外的情况需要特殊处理.比如PagerTabStrip无法在布局文件中指定文本大小和文本颜色,只能 ...

  9. Caused by:org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type "" available: expected at least 1 bean which qualifies as autowire candidate

    项目使用spring, mybatis.因为分了多个模块,所以会这个模块引用了其它模块的现在,结果使用Junit测试的时候发现有两个模块不能自动注入dao和service问题.解决后在此记录一下. 解 ...

  10. MySQL 授予普通用户PROCESS权限

    在MySQL中如何给普通用户授予查看所有用户线程/连接的权限,当然,默认情况下show processlist是可以查看当前用户的线程/连接的. mysql> grant process on ...