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 ...
随机推荐
- MYSQL数据库性能调优之七:其他(读写分离、分表等)
一.分表 水平划分 垂直划分 二.读写分离 三.选择合理的数据类型 特别是主键 四.文件.图片等大文件使用文件系统存储 五.数据库参数配置 注意:max_connections最大连接数一般设置在10 ...
- HTTP中缓存相关
1.客户端如何区分缓存命中和未命中 两种情况下,返回的状态码都是200,客户端有一个方法可以判断,就是使用Date首部,将Date首部与当前时间进行比较,如果响应中时间日期值比较早,客户端可以认为这是 ...
- 【全面完美方案】iPhone 4S WiFi变灰 DIY修复方式
这是我在一位台湾网友usaretama发表的一篇帖子中看到的,原帖我发表在维维网 如果你有WiFi开关变灰不能切换.WiFi遇到搜不到AP或搜到了连不上,那您就要注意这篇了. 家人的 iPhone 4 ...
- 使用python获得git中分支存成list
通过这个搜集git工程下的branch信息例子,来说明一下python和终端的信息交互,和字符串的简单处理.代码如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
- HDU 3586 Information Disturbing (二分+树形dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3586 给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏 ...
- (博弈论)hdoj 1525 Euclid's Game
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1525 题目大意:Stan和Ollie在玩一个游戏,a和b两个数字,每次可以选择较大的数减去较小的数的若 ...
- MRI中T1和T2的含义与区分[转]
A. MRI名词解释 T1加权像.T2加权像为磁共振检查中报告中常提到的术语,很多非专业人士不明白是什么意思,要想认识何为T1加权像.T2加权像,请先了解几个基本概念: 1.磁共振(maget ...
- C#.Net中的非托管代码清理
帮助其它项目组Review代码过程,发现有些地方实现了IDispose接口,同时也发现了一些关于IDispose的问题: 1.A类型实现了IDispose接口,B类型里面含有A类型的字段,B类型没有实 ...
- System.Data.SQLite数据库简介
SQLite介绍 在介绍System.Data.SQLite之前需要介绍一下SQLite,SQLite是一个类似于Access的单机版数据库管理系统,它将所有数据库的定义(包括定义.表.索引和数据本身 ...
- C++学习笔记之字符函数库cctype
C++从C语言继承了一个与字符相关的.非常方便的函数软件包,它可以简化诸如确定字符是否为大写字母.数字.标点符号等工作,这些函数原型是在头文件cctype(老式风格ctype.h)中定义的. 下表对这 ...