解题报告 之 UVA11134 Fabled Rooks

Description

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 (xliyli) and its right-lower corner (xriyri), 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,ylixri, 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

题目大意:n*n的棋盘上有n个车,使得任意两个车不在同一列且任意两个车不在同一行。且每个车的位置在给出的两个坐标为对角线的矩形内。试构造出一种摆法。

这一题的主要注意到的是行和列没关系,按照书上的说法就是问题分解,分别考虑行和列的情况,而且是对称的。所以将二维问题分解为两个一维问题,用贪心法解题。

先考虑行,我们按照范围起点大小排序,然后尽可能输出比较靠前的位置(给后面的点留下空间)。但第二个坑点就是两个车的行范围可能有重复,如果在范围起点一致的情况下,要先处理范围小的(即范围终点),否则有可能就会误操作。所以我排序的方法是采用先按照范围起点(xl)排序,再按照范围终点(xr)排序。但是会出一个问题就是前面的某个点被占用了之后其他的点并没有更新其起点,所以我又加了一个关于m的限制,然后每一个车选择完位置之后再将未确定位置的车排序一次(其目的是更新起点)。
然后上代码:
#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相关推荐

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

  2. UVa11134 Fabled Rooks(贪心算法)

    问题:在n*n的棋盘中,放置n个车,要求对应的车在规定的矩形区间范围内,并且n个车不在同一行或者列上. 思路: 从x,y方向上分别确认n个车的位置,以x方向为例.根据区间的右端从小到大排列.然后在对车 ...

  3. UVA11134 Fabled Rooks

    摘要: 贪心 链接 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  4. uva11134 -Fabled Rooks

    题意: 一个n*n的矩阵上放n个车,第i辆车在第i个区间上,每个区间给出左上角和右下角的坐标.任意两个车之间同行同列不能互相攻击,求这些车放置 的坐标. 思路: 第一眼看以为是N皇后的变形,后来发现车 ...

  5. 训练指南第一部分解题报告

    主要是提供训练指南第一部分解题报告链接,后面会持续更新中 307 - Sticks  (DFS+剪枝) 11292 - Dragon of Loowater (贪心) 11729 - Commando ...

  6. uscao 线段树成段更新操作及Lazy思想(POJ3468解题报告)

    线段树成段更新操作及Lazy思想(POJ3468解题报告) 标签: treequerybuildn2cstruct 2011-11-03 20:37 5756人阅读 评论(0) 收藏 举报  分类: ...

  7. 解题报告(十八)数论题目泛做(Codeforces 难度:2000 ~ 3000 + )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...

  8. 【解题报告系列】超高质量题单 + 题解(ACM / OI)超高质量题解

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我新写的超高质量的题解和代码,题目难度不 ...

  9. 解题报告(三)多项式求值与插值(拉格朗日插值)(ACM / OI)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量的题解和代码,题目难度不一 ...

最新文章

  1. linux开机自动执行命令或自动启动程序(rc.local)
  2. centos ffmpeg m3u8切片相关
  3. 2.3_模型和交叉检验
  4. 服务器2003系统U盘安装方法,u启动windows2003PE工具箱
  5. 程序员应该知道的Mac工具
  6. 关于概要设计文档的写作
  7. 【运筹学】线性规划数学模型 ( 求解基矩阵示例 | 矩阵的可逆性 | 线性规划表示为 基矩阵 基向量 非基矩阵 非基向量 形式 )
  8. 制作u盘winpe启动盘_RUFUS.小巧的U盘启动盘制作工具
  9. tplink怎么进去_怎么进入TP-Link路由器设置界面?
  10. iGoogle创新设计大赛
  11. 用计算机计算的加减乘除题目,在电脑上怎么做加减乘除算术题
  12. AEG 2A 400-280 HFRL1
  13. 人工智能前景怎么样 用哪个开发语言比较好
  14. Android App接入支付功能——支付宝支付
  15. 02.微软官方启动U盘装机教程
  16. SSM项目秒杀系统---(一)业务分析与Dao层
  17. python学习笔记——字符串操作
  18. inl和dnl matlab_matlab仿真inl dnl
  19. pandas笔记2---reset_index函数drop与inplace参数的理解
  20. jdbc批处理+手动事务+多线程实现81秒插入1000万数据(多线程版)

热门文章

  1. 微信定位精灵服务器或网络异常,微信定位精灵系统界面无法更新怎么办
  2. JAVA基础 之 String
  3. java的可执行文件_java生成可执行文件的方法总结
  4. Web安全常见基本知识
  5. 如何在linux中备份文件
  6. RubyProgramming:向Ruby之父学程序设计pdf
  7. python对mysql增删改查+计算器+九九乘法表
  8. APK查壳软件(根据so名)
  9. 团队形成的要经历的5个阶段
  10. 几何光学学习笔记(7)- 3.1 理想光学系统