问题来源:刘汝佳《算法竞赛入门经典--训练指南》 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 (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 xliylixri, 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 贪心问题)的更多相关文章

  1. UVa 11134 - Fabled Rooks 优先队列,贪心 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

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

  3. uva 11134 - Fabled Rooks(问题转换+优先队列)

    题目链接:uva 11134 - Fabled Rooks 题目大意:给出n,表示要在n*n的矩阵上放置n个车,并且保证第i辆车在第i个区间上,每个区间给出左上角和右小角的坐标.另要求任意两个车之间不 ...

  4. UVA 11134 Fabled Rooks 贪心

    题目链接:UVA - 11134 题意描述:在一个n*n(1<=n<=5000)的棋盘上放置n个车,每个车都只能在给定的一个矩形里放置,使其n个车两两不在同一行和同一列,判断并给出解决方案 ...

  5. 贪心 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 ...

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

  7. uva11134 - Fabled Rooks(问题分解,贪心法)

    这道题非常好,不仅用到了把复杂问题分解为若干个熟悉的简单问题的方法,更是考察了对贪心法的理解和运用是否到位. 首先,如果直接在二维的棋盘上考虑怎么放不好弄,那么注意到x和y无关(因为两个车完全可以在同 ...

  8. UVA - 11134 Fabled Rooks(传说中的车)(贪心)

    题意:在n*n的棋盘上放n个车,使得任意两个车不相互攻击,且第i个车在一个给定的矩形Ri之内,不相互攻击是指不同行不同列,无解输出IMPOSSIBLE,否则分别输出第1,2,……,n个车的坐标. 分析 ...

  9. UVA 11134 - Fabled Rooks(贪心+优先队列)

    We would like to place  n  rooks, 1 ≤  n  ≤ 5000, on a  n×n  board subject to the following restrict ...

随机推荐

  1. 【原创】Silverlight客户端发起WebRequest请求分析

    Silverlight网站部署后,客户端浏览器访问的时候会 下载 网站的xap文件包等信息,把程序代码放到本地执行,因为本地机器上安装了silverlight运行库. 所以如果silverlight前 ...

  2. WPF后台设置xaml控件的样式System.Windows.Style

    WPF后台设置xaml控件的样式System.Windows.Style 摘-自 :感谢 作者: IT小兵   http://3w.suchso.com/projecteac-tual/wpf-zhi ...

  3. 参数化命令相关知识点(防止Sql注入)

    一: 使用参数化命令查询DAL类: public DataTable StudentDAL(string name,string gender) { string str="连接字符串&qu ...

  4. sqlsever备份,还原和导入导出方法

    一:sqlsever数据库的几种备份和还原方法: 第一种:备份还原方法 1.选择要备份的数据库-任务-备份- 打开之后会看到里面有: 常规:在常规页面中我们可以看到:右下角位置有一个可以更改存储(备份 ...

  5. C#按回车Enter使输入焦点自动跳到下一个TextBox的方法收集

    在录入界面中,用户往往需要按回车键时光标自动跳入下一个文本框,以方便录入操作.在C#中实现该功能有多种方法,以下是小编收集的不使用TAB键,而直接用回车键将光标转到下一个文本框的实现方法. 一.利用W ...

  6. csharp: Sound recording

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsof ...

  7. n个人作m幅画

    题目网址: http://codeforces.com/problemset/problem/416/B A well-known art union called "Kalevich is ...

  8. js removeChild 方法

    1. 概述 删除后的节点虽然不在文档树中了,但其实它还在内存中,可以随时再次被添加到别的位置. 当你遍历一个父节点的子节点并进行删除操作时,要注意,children属性是一个只读属性,并且它在子节点变 ...

  9. html 隐藏滚动条

    1. html 标签加属性 <html lang="en" class="no-ie" style="overflow:hidden;" ...

  10. [python拾遗]列表

    python列表拾遗 1.列表可以修改,使用 ‘+’ 将一个新列表附加在原列表的尾部: >>> a = [1,'a'] >>> b = a + [2,'b'] &g ...