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 ...
随机推荐
- 禁止苹果浏览器Safari将数字识别成电话号码的方法
偶然发现用ipad访问我的网站时,发现网站上的一串数字变颜色了(原来是红色的),现在变成了蓝色.一开始以为网站出了什么问题,后来在PC端查看,发现颜色依旧是红色.在ipad上点击还会弹出菜单呼叫的选项 ...
- datagrid动态数据添加超链接的方法
首先,我我们要有一个json格式的datagrid_data.json文件,如下:
- EasyUI个人项目晒图(续)
晒自己做的一个管理系统(清新风格)EasyUI 这是自己上一次的文章了,只是给大家看一下自己的美观度是不是还是停留在新手的阶段!反正我自己认为我已经不是一个新手了吧!虽然技术永远学不完,我可以说,我和 ...
- [moka同学笔记]yii2 activeForm 表单样式的修改(二)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABAEAAANXCAIAAADLkdErAAAgAElEQVR4nOzdfWwc953nef6zwO5Zg8
- Python tools for Visual Studio插件介绍
Python tools for Visual Studio是一个免费开源的VisualStudio的插件,支持 VisualStudio 2010,2012与2013.我们想要实现的是: ...
- Oracle不足与MySQL优势
Oracle库主要不足:1.单点故障:2.付费License;3.不支持水平扩展. MySQL及水平拆库的优势:1.单点故障影响率1/N:2.免费:3.可低成本水平扩展,近乎无限的水平扩展能力.
- JVM的ClassLoader过程分析
本文来自网络:深入分析Java ClassLoader原理 http://my.oschina.net/zhengjian/blog/133836 一. JVM的ClassLoader过程以及装载原理 ...
- javascript --- 设计模式之单体模式(二)
在JavaScript里,实现单例的方式有很多种,其中最简单的一个方式是使用对象字面量的方法,其字面量里可以包含大量的属性和方法: var her = { property1: 'someing', ...
- RHEL7进程管理
进程概念 名称 说明 程序 一组指令的集合 进程 程序的执行就是进程也可以把进程看成一个独立的程序在内存中有其对应的代码空间和数据空间,一个进程所拥有的数据和代码只属于自己进程是资源分配的基本单位,也 ...
- 网站SEO之百度优化不得不知的铁人三项规则
奥运会有铁人三项,此运动更好的协调了运动员的综合素质水平,而百度优化排名中的“铁人三项”规则则是让网站的整体质量更好的满足市场用户体验.针对不同部分的操作,可以让网站在每个细节处都能凸显以人为本的服务 ...