Problem UVA11134-Fabled Rooks(贪心)
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
Sample Input
#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(贪心)的更多相关文章
- UVA-11134 Fabled Rooks 贪心问题(区间贪心)
题目链接:https://cn.vjudge.net/problem/UVA-11134 题意 在 n*n 的棋盘上,放上 n 个车(ju).使得这 n 个车互相不攻击,即任意两个车不在同一行.同一列 ...
- uva11134 - Fabled Rooks(问题分解,贪心法)
这道题非常好,不仅用到了把复杂问题分解为若干个熟悉的简单问题的方法,更是考察了对贪心法的理解和运用是否到位. 首先,如果直接在二维的棋盘上考虑怎么放不好弄,那么注意到x和y无关(因为两个车完全可以在同 ...
- 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 ...
- UVA 11134 - Fabled Rooks(贪心+优先队列)
We would like to place n rooks, 1 ≤ n ≤ 5000, on a n×n board subject to the following restrict ...
- Uva11134 Fabled Rooks
普通的贪心题. 虽然图是二维的,但可以把横向和纵向分开处理. 将区间按右端点排序,然后从区间左端点到右端点找第一个空位置放棋子即可. /*by SilverN*/ #include<algori ...
- UVA 11134 Fabled Rooks 贪心
题目链接:UVA - 11134 题意描述:在一个n*n(1<=n<=5000)的棋盘上放置n个车,每个车都只能在给定的一个矩形里放置,使其n个车两两不在同一行和同一列,判断并给出解决方案 ...
- 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 ...
- 贪心 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 ...
- 01_传说中的车(Fabled Rooks UVa 11134 贪心问题)
问题来源:刘汝佳<算法竞赛入门经典--训练指南> P81: 问题描述:你的任务是在n*n(1<=n<=5000)的棋盘上放n辆车,使得任意两辆车不相互攻击,且第i辆车在一个给定 ...
- UVA 11134 Fabled Rooks(贪心的妙用+memset误用警示)
题目链接: https://cn.vjudge.net/problem/UVA-11134 /* 问题 输入棋盘的规模和车的数量n(1=<n<=5000),接着输入n辆车的所能在的矩阵的范 ...
随机推荐
- Java_Comparable,Comparator两接口区别
Comparable和Comparator的区别 根本区别 1.Comparable是一个内比较器,Comparator是一个外比较器 封装的包不同 java.util.Comparator java ...
- 3. mysql性能分析
一.mysql query optimizer 1. mysql 中有专门负责优化 select 语句的优化器模块,主要功能:通过计算分析系统中收集的统计信息,为客户端的 Query 提供他认为最优的 ...
- Math.max()/min()
返回一组数中最大值: 找到数组中的最大值,有两种方法,一种是apply,一种使用拓展运算符. 释义: 由于max()里面参数不能为数组,所以借助apply(funtion,args)方法调用Math. ...
- setTimeout()与clearTimeout()
setTimeout(code,millisec)setTimeout() 只执行 code 一次.如果要多次调用,请使用 setInterval() 或者让 code 自身再次调用 setTimeo ...
- nodeJs express mongodb 建站(window 10 版)
一.环境搭建 安装 node.git.npm.express.mongodb.主要介绍express.mongodb 的安装. (1)node安装:https://nodejs.org/en/down ...
- 客户化软件时代的前夜 ZT
制造业:从手工模式到大规模生产,再到大规模定制 工业革命开始以后,机器全面代替了手工工具.随着工业经济的不断发展,机器的使用导致了两种截然不同的方式.一种是手工生产基本思想的延续,另一种则是大规模生产 ...
- JMeter java.net.URISyntaxException:Illegalcharacterinquery解决方案
java.net.URISyntaxException: Illegal character in query解决方案 by:授客 QQ:1033553122 测试环境 apache-jmeter ...
- Python高级特性:迭代
迭代的目的是实现遍历出一个可迭代对象的元素,即for循环 基本语法 : for ... in ... 首先只有可迭代对象才可以迭代,判断一个对象是不是可以迭代的方法如下: >>> f ...
- css和HTML布局小技巧
一:水平居中 1. 如下所示,让child在parent中水平居中 <!DOCTYPE html> <html> <head lang="en"> ...
- mac 苹果多版本jdk自由切换
场景 手头上的工具有时候依赖低版本jdk,有时候需要高版本jdk, 如何在不同版本jdk之间来回自由的切换? 安装 首选需要去官网下载dmg安装包,地址:https://www.oracle.com/ ...