01_传说中的车(Fabled Rooks UVa 11134 贪心问题)
问题来源:刘汝佳《算法竞赛入门经典--训练指南》 P81:
问题描述:你的任务是在n*n(1<=n<=5000)的棋盘上放n辆车,使得任意两辆车不相互攻击,且第i辆车在一个给定的矩形R之内。
问题分析:1.题中最关键的一点是每辆车的x坐标和y坐标可以分开考虑(他们互不影响),不然会变得很复杂,则题目变成两次区间选点问题:使得每辆车在给定的范围内选一个点,任何两辆车不能选同一个点。
2.本题另外一个关键点是贪心法的选择,贪心方法:对所有点的区间,按右端点从小到大排序;每次在一个区间选点的时候,按从左到右选没有被前面区间选过的点。(从这个区间开始选最大程度的防止了以后的区间没有点可以选(因为右端点选的是最小的))
错误的贪心方法:把所有区间按左端排序,然后每次选能选的最左边的。反例:[1,1],[1,3],[2,2];(这种贪心发并不能保证以后的区间有点可以选,某些区间可能更长,取后面的点更合适)
例题链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2075
例题:UVa 11134
11134 - Fabled Rooks
Time limit: 3.000 seconds
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, 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 output n 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 "stdio.h"
#include "string.h"
#include "algorithm"
using namespace std; #define N 5010 typedef struct
{
int id;
int l,r;
}Point; int n;
bool mark[N];
Point x[N],y[N],ans[N];
Point ansx[N],ansy[N]; bool cmp(Point a,Point b) { return a.r < b.r; } //按右端点最小的进行排序 bool cmp1(Point a,Point b){ return a.id < b.id;} //按id号还原顺序 bool solve(Point *a,Point *ans)
{
int i,j;
memset(mark,false,sizeof(mark));
for(i=; i<n; i++)
{
for(j=a[i].l; j<=a[i].r; j++)
{
if(mark[j]) continue;
break;
}
if(j>a[i].r) return false;
ans[i].l = j; //用ans[i].l保存答案
ans[i].id = a[i].id;
mark[j] = true;
}
return true;
} int main()
{
int i;
while(~scanf("%d",&n),n!=)
{
for(i=; i<n; i++)
{
scanf("%d %d %d %d",&x[i].l,&y[i].l,&x[i].r,&y[i].r);
x[i].id = y[i].id = i;
}
sort(x,x+n,cmp);
sort(y,y+n,cmp);
if(solve(x,ansx) && solve(y,ansy))
{
sort(ansx,ansx+n,cmp1);
sort(ansy,ansy+n,cmp1);
for(i=; i<n; i++)
printf("%d %d\n",ansx[i].l,ansy[i].l);
}
else
printf("IMPOSSIBLE\n");
}
return ;
}
01_传说中的车(Fabled Rooks UVa 11134 贪心问题)的更多相关文章
- UVa 11134 - Fabled Rooks 优先队列,贪心 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- 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 11134 Fabled Rooks 贪心
题目链接:UVA - 11134 题意描述:在一个n*n(1<=n<=5000)的棋盘上放置n个车,每个车都只能在给定的一个矩形里放置,使其n个车两两不在同一行和同一列,判断并给出解决方案 ...
- 贪心 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 ...
- 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 ...
- uva11134 - Fabled Rooks(问题分解,贪心法)
这道题非常好,不仅用到了把复杂问题分解为若干个熟悉的简单问题的方法,更是考察了对贪心法的理解和运用是否到位. 首先,如果直接在二维的棋盘上考虑怎么放不好弄,那么注意到x和y无关(因为两个车完全可以在同 ...
- UVA - 11134 Fabled Rooks(传说中的车)(贪心)
题意:在n*n的棋盘上放n个车,使得任意两个车不相互攻击,且第i个车在一个给定的矩形Ri之内,不相互攻击是指不同行不同列,无解输出IMPOSSIBLE,否则分别输出第1,2,……,n个车的坐标. 分析 ...
- UVA 11134 - Fabled Rooks(贪心+优先队列)
We would like to place n rooks, 1 ≤ n ≤ 5000, on a n×n board subject to the following restrict ...
随机推荐
- C#类的继承相关总结
1.子类继承父类,会拥有父类中所规范的所有成员,但是只能是使用其中的公共成员 2.实现了继承,可以做到代码的冗余,做到代码的重用 3.实现了继承,可以方便代码的扩展与修改 4,当子类拥有与父类相同签名 ...
- JavaScript基础概念
1.JavaScript在浏览器中是解释执行的: 2.JavaScript是一中弱类型的语言,在使用变量前,可以不用先申明: 3.JavaScript使用了对象对象程序设计思想: 4.JavaScri ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- Linq查询操作之Where筛选
筛选操作where能够处理逻辑运算符组成的逻辑表达式.比如逻辑“与”,逻辑“或”,并从数据源中筛选数据,它和where子句的功能非常相似.Enumerable类的Where()原型如下: public ...
- appt查看apk信息
aapt dump badging app-debug.apk
- ext 树节点操作
ext 树节点操作 tree :树 node:节点 1.全部展开 tree.expandAll(); 2.全部收缩 tree.collapseAll(); 3.得到父节点 node.parent ...
- C# 生成XML空元素/空节点自动换行解决方案
使用DataSet可以直接输出XML,并可指定是否带有Schema: ds.WriteXml(XMLFile,XmlWriteMode.WriteSchema ) 不过,这样将不会输出值为Null的字 ...
- XmlNodeList循环读取节点值
foreach (XmlNode item in XmlNodeList) { string oid = item.SelectSingleNode("oid").InnerTex ...
- Ajax,谷歌提示AutoCompleteExtender控件
提示内容从数据库中读取: ------------------------------------------页面 <asp:ScriptManager ID="ScriptManag ...
- csharp: json to csharp
http://json2csharp.com/ http://jsonclassgenerator.codeplex.com/ http://jsonutils.com/ JSON生成类文件 http ...