解题报告 之 UVA11134 Fabled Rooks
解题报告 之 UVA11134 Fabled Rooks
Description
![](http://uva.onlinejudge.org/components/com_onlinejudge/images/button_pdf.png)
Problem F: Fabled Rooks
We would like to place n rooks, 1 ≤ n ≤ 5000, on a n×nboard 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 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 <algorithm>
#define MAXN 5010
using namespace std;struct car
{int xl, yl, xr, yr;int x, y;int order;
};car cars[MAXN];
int m;bool cmp1(const car& lhs, const car& rhs)
{int tem1 = lhs.xl > m ? lhs.xl : m + 1;int tem2 = rhs.xl > m ? rhs.xl : m + 1;if (tem1 == tem2) return lhs.xr < rhs.xr;return tem1 < tem2;
}bool cmp2(const car& lhs, const car& rhs)
{int tem1 = lhs.yl > m ? lhs.yl : m + 1;int tem2 = rhs.yl > m ? rhs.yl : m + 1;if (tem1 == tem2) return lhs.yr < rhs.yr;return tem1 < tem2;
}bool cmp3(const car& lhs, const car& rhs)
{return lhs.order < rhs.order;
}int main()
{int n;while (cin >> n&&n){for (int i = 0; i < n; i++){cin >> cars[i].xl >> cars[i].yl >> cars[i].xr >> cars[i].yr;cars[i].order = i;}m = 0;int flag = true;for (int i = 0; i < n; i++){sort(cars + i, cars + n, cmp1);if (m < cars[i].xr){cars[i].x = m = max(m + 1, cars[i].xl);}else{flag = false;break;}}if (!flag){cout << "IMPOSSIBLE" << endl;continue;}flag = true, m = 0;for (int i = 0; i < n; i++){sort(cars + i, cars + n, cmp2);if (m < cars[i].yr){cars[i].y = m = max(m + 1, cars[i].yl);}else{flag = false;break;}}if (!flag){cout << "IMPOSSIBLE" << endl;continue;}sort(cars, cars + n, cmp3);for (int i = 0; i < n; i++)cout << cars[i].x << " " << cars[i].y << endl;}
}
哎呀一周19道看来的确是吃不消啊,,队长你看着办。
解题报告 之 UVA11134 Fabled Rooks相关推荐
- UVa11134 - Fabled Rooks(贪心)
11134 - Fabled Rooks We would like to place nn rooks, 1≤n≤50001 ≤ n ≤ 5000, on a n×nn × n board subj ...
- UVa11134 Fabled Rooks(贪心算法)
问题:在n*n的棋盘中,放置n个车,要求对应的车在规定的矩形区间范围内,并且n个车不在同一行或者列上. 思路: 从x,y方向上分别确认n个车的位置,以x方向为例.根据区间的右端从小到大排列.然后在对车 ...
- UVA11134 Fabled Rooks
摘要: 贪心 链接 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- uva11134 -Fabled Rooks
题意: 一个n*n的矩阵上放n个车,第i辆车在第i个区间上,每个区间给出左上角和右下角的坐标.任意两个车之间同行同列不能互相攻击,求这些车放置 的坐标. 思路: 第一眼看以为是N皇后的变形,后来发现车 ...
- 训练指南第一部分解题报告
主要是提供训练指南第一部分解题报告链接,后面会持续更新中 307 - Sticks (DFS+剪枝) 11292 - Dragon of Loowater (贪心) 11729 - Commando ...
- uscao 线段树成段更新操作及Lazy思想(POJ3468解题报告)
线段树成段更新操作及Lazy思想(POJ3468解题报告) 标签: treequerybuildn2cstruct 2011-11-03 20:37 5756人阅读 评论(0) 收藏 举报 分类: ...
- 解题报告(十八)数论题目泛做(Codeforces 难度:2000 ~ 3000 + )
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...
- 【解题报告系列】超高质量题单 + 题解(ACM / OI)超高质量题解
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我新写的超高质量的题解和代码,题目难度不 ...
- 解题报告(三)多项式求值与插值(拉格朗日插值)(ACM / OI)
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...
最新文章
- linux开机自动执行命令或自动启动程序(rc.local)
- centos ffmpeg m3u8切片相关
- 2.3_模型和交叉检验
- 服务器2003系统U盘安装方法,u启动windows2003PE工具箱
- 程序员应该知道的Mac工具
- 关于概要设计文档的写作
- 【运筹学】线性规划数学模型 ( 求解基矩阵示例 | 矩阵的可逆性 | 线性规划表示为 基矩阵 基向量 非基矩阵 非基向量 形式 )
- 制作u盘winpe启动盘_RUFUS.小巧的U盘启动盘制作工具
- tplink怎么进去_怎么进入TP-Link路由器设置界面?
- iGoogle创新设计大赛
- 用计算机计算的加减乘除题目,在电脑上怎么做加减乘除算术题
- AEG 2A 400-280 HFRL1
- 人工智能前景怎么样 用哪个开发语言比较好
- Android App接入支付功能——支付宝支付
- 02.微软官方启动U盘装机教程
- SSM项目秒杀系统---(一)业务分析与Dao层
- python学习笔记——字符串操作
- inl和dnl matlab_matlab仿真inl dnl
- pandas笔记2---reset_index函数drop与inplace参数的理解
- jdbc批处理+手动事务+多线程实现81秒插入1000万数据(多线程版)