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 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, andyri. 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 <cstdio>
- #include <cstring>
- //#include <cmath> 命名冲突 y1
- #include <algorithm>
- #include <string>
- #include <vector>
- #include <stack>
- #include <queue>
- #include <set>
- #include <map>
- #include <list>
- #include <iomanip>
- #include <cstdlib>
- #include <sstream>
- using namespace std;
- typedef long long LL;
- const int INF=0x5fffffff;
- const double EXP=1e-;
- const int MS=;
- int x1[MS], y1[MS], x2[MS], y2[MS], x[MS], y[MS];
- /*
- 先将各个车分配在同一列的不同行,然后分配不同的列,
- 使他们彼此错开,任意两个车不在同一列和同一行。
- 也就是说行和列的分配时可以分开的。或者说独立的
- 使用贪心法分配。
- */
- bool solve(int *a,int *b,int *c,int n)
- {
- // memset(c,-1,sizeof(c)); 注意这样是错误的,因为不知道c到哪里结束。字符串指针才可以,因为有结束符
- fill(c,c+n,-); // ==-1表示还没有分配
- for(int col=;col<=n;col++)
- {
- int rook=-,minb=n+;
- for(int i=;i<n;i++)
- {
- if(c[i]<&&col>=a[i]&&b[i]<minb)
- {
- rook=i;
- minb=b[i];
- }
- }
- if(rook<||col>minb)
- return false;
- c[rook]=col;
- }
- return true;
- }
- int main()
- {
- int n;
- while(scanf("%d",&n)&&n)
- {
- for(int i=;i<n;i++)
- scanf("%d%d%d%d",&x1[i],&y1[i],&x2[i],&y2[i]);
- if(solve(x1,x2,x,n)&&solve(y1,y2,y,n))
- for(int i=;i<n;i++)
- printf("%d %d\n",x[i],y[i]);
- else
- printf("IMPOSSIBLE\n");
- }
- return ;
- }
L - Fabled Rooks(中途相遇法和贪心)的更多相关文章
- UVA - 11134 Fabled Rooks问题分解,贪心
题目:点击打开题目链接 思路:为了满足所有的车不能相互攻击,就要保证所有的车不同行不同列,于是可以发现,行与列是无关的,因此题目可以拆解为两个一维问题,即在区间[1-n]之间选择n个不同的整数,使得第 ...
- 01_传说中的车(Fabled Rooks UVa 11134 贪心问题)
问题来源:刘汝佳<算法竞赛入门经典--训练指南> P81: 问题描述:你的任务是在n*n(1<=n<=5000)的棋盘上放n辆车,使得任意两辆车不相互攻击,且第i辆车在一个给定 ...
- 贪心 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 ...
- 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(问题转换+优先队列)
题目链接:uva 11134 - Fabled Rooks 题目大意:给出n,表示要在n*n的矩阵上放置n个车,并且保证第i辆车在第i个区间上,每个区间给出左上角和右小角的坐标.另要求任意两个车之间不 ...
- uva 6757 Cup of Cowards(中途相遇法,貌似)
uva 6757 Cup of CowardsCup of Cowards (CoC) is a role playing game that has 5 different characters (M ...
- LA 2965 Jurassic Remains (中途相遇法)
Jurassic Remains Paleontologists in Siberia have recently found a number of fragments of Jurassic pe ...
- 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 高效算法——J 中途相遇法,求和
---恢复内容开始--- J - 中途相遇法 Time Limit:9000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Su ...
随机推荐
- hdu 5655 CA Loves Stick
CA Loves Stick Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) ...
- json的一些问题
使用json不仅可以这么写,{"ARCHIVAL_CODE":"String","TDQLR":"String"} 还可 ...
- Session和Cookie的分析与区别
首先说一下Web.config文件中的cookieless="false"的理解 cookieless="false"表示: 如果用户浏览器支持cookie时启 ...
- C# 动态绘制任务栏图标的实现
通常我们在做一个应用时会遇到这样的需求:将收到的消息条数显示到任务栏,比如如下的效果 怎么实现呢? 答案是采用WindowsAPICodePack实现,具体参见:Windows 7 任务栏开发 之 覆 ...
- sc7731 Android 5.1 LCD驱动简明笔记之三
此篇笔记基于sc7731 - android 5.1,对lcd的gralloc库做一个简明笔记. 第一部分 调用gralloc.sc8830.so所谓的Gralloc模块,它就是一个模块,一个操作ke ...
- Codeforces Round #309 (Div. 2) B. Ohana Cleans Up 字符串水题
B. Ohana Cleans Up Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/554/pr ...
- hdu 5265 pog loves szh II STL
pog loves szh II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...
- 使用.net(C#)发送邮件学习手册(带成功案例)
使用.net(C#)发送邮件学习手册(带成功案例) 1.了解发送邮件的三种方式 2.实例介绍使用client.DeliveryMethod = System.Net.Mail.SmtpDelivery ...
- Delphi开发OCX详细步骤总结
首先要弄明白你要写的OCX是用在客户端还是用在服务器端 假如用在客户端: 1.创建 打开delphi 7,选择菜单"new"->"other"- ...
- 如何实现一个c/s模式的flv视频点播系统
一.写在前面 视频点播,是一个曾经很热,现如今依然很热的一项视频服务技术.本人最近致力于研究将各种视频格式应用于点播系统中,现已研究成功FLV, F4V, MP4, TS格式的视频点播解决方案,完全支 ...