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. 【Linux命令】grep命令

    1.作用 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全 ...

  2. Hibernate(十四)抓取策略

    抓取策略: 抓取策略是当应用程序需要在(Hibernate实体对象图的)关联关系间进行导航的时候,Hibernate如何获取关联对象的策略.Hibernate的抓取策略是Hibernate提升性能的一 ...

  3. SqlHelper模板

    在实际开发中,我们不会直接使用拼写SQL语句的方法进行数据库操作,而是使用参数化的方法进行数据库操作,这样做的好处很多,不仅提高了程序的健壮性,同时也避免的SQL注入的问题.在这里,笔者为初学者提供一 ...

  4. Java8 默认方法

    概述 Java8新增了接口的默认方法.使用default关键字. 默认方法就是接口可以有实现方法,而且不需要实现类来实现其方法.相对于JDK1.8之前的接口来说,新增了可以接口中实现方法. 可以说在接 ...

  5. ReactDom

    今天工作中使用了这个,感觉很好用啊! 首先: 这个ReactDom是干嘛用的? 答:   react-dom 包提供了 DOM 特定的方法,可以在你的应用程序的顶层使用,如果你需要的话,也可以作为 R ...

  6. JS中的反柯里化( uncurrying)

    反柯里化 相反,反柯里化的作用在与扩大函数的适用性,使本来作为特定对象所拥有的功能的函数可以被任意对象所用.即把如下给定的函数签名, obj.func(arg1, arg2) 转化成一个函数形式,签名 ...

  7. iOS--------获取当前连接的WiFi以及IP地址

    导入头文件 #import <ifaddrs.h>#import <arpa/inet.h>#import <SystemConfiguration/CaptiveNet ...

  8. Ne10编译安装

    介绍 NEON,即"ARM Advanced SIMD",是ARM从ARMv7开始提供的高级单指令多数据(SIMD)扩展.它是一种64/128位混合SIMD体系结构.NEON在网上 ...

  9. Spring整合ActiveMq消息队列

    ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久 ...

  10. javascript的隐式类型转换

    首先简单了解js的typeof,会返回六种类型 即 number string boolen function object undefined 也就是六种基本数据类型 显示类型转换大概有以下几种: ...