11134 - Fabled Rooks

We would like to place nn rooks, 1≤n≤50001 ≤ n ≤ 5000, on a n×nn × n board subject to the following restrictions
• The i-th rook can only be placed within the rectangle given by its left-upper corner (xlix_{li} , yliy_{li}) and its rightlower corner (xri,yrix_{ri} , y_{ri}), where 1≤i≤n,1≤xli≤xri≤n,1≤yli≤yri≤n1 ≤ i ≤ n, 1 ≤ x_{li} ≤ x_{ri} ≤ n, 1 ≤ y_{li} ≤ y_{ri} ≤ n.
• No two rooks can attack each other, that is no two rooks can occupy the same column or the same row.

Input

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,x_{li} , y_{li} , x_{ri} , and yri y_{ri}
. The input file is terminated with the integer ‘0’ on a line by itself.

Output

Your task is to find such a placing of rooks that the above conditions are satisfied and then output n<script type="math/tex" id="MathJax-Element-277">n</script> 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

Sample Output

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

题意

类似八皇后问题,区别是没有了斜45度方向的限制,数据范围是5000,并且每个棋子只会出现在给定的矩形区域。

题解

可以看出行和列的分布是互不影响的,问题可分解为两个一维空间的分布问题。贪心的过程是从区间的一个端点开始贪心,比如现在要找x的分布,那么从第一列开始贪心,找到所有可以放在第一列的棋子中r值最小的,因为r值小,所以这个棋子可以选择的位置更少,如果换成一个r值更大的棋子结果不会更优。(代码写得比较丑。。跑了600ms)

代码

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#define fout freopen("out.txt","w",stdout)
#define fin freopen("in.txt","r",stdin)using namespace std;int n;
int now;
int mark;
struct node
{int l,r,id;bool operator<(const node &R)const{if(l<=now&&R.l<=now)return r<R.r;return l<=now;}
};
node A[5005],B[5005];
int x[5005],y[5005];
int main()
{while(scanf("%d",&n)&&n!=0){mark=1;for(int i=1;i<=n;i++){A[i].id=B[i].id=i;scanf("%d%d%d%d",&A[i].l,&B[i].l,&A[i].r,&B[i].r);}for(now=1;now<=n;now++){sort(A+now,A+n+1);sort(B+now,B+n+1);if(A[now].l>now||A[now].r<now||B[now].l>now||B[now].r<now){printf("IMPOSSIBLE\n");mark=0;break;}x[A[now].id]=now;y[B[now].id]=now;}if(mark){for(int i=1;i<=n;i++)printf("%d %d\n",x[i],y[i]);}}return 0;
}

UVa11134 - Fabled Rooks(贪心)相关推荐

  1. UVa11134 Fabled Rooks(贪心算法)

    问题:在n*n的棋盘中,放置n个车,要求对应的车在规定的矩形区间范围内,并且n个车不在同一行或者列上. 思路: 从x,y方向上分别确认n个车的位置,以x方向为例.根据区间的右端从小到大排列.然后在对车 ...

  2. 解题报告 之 UVA11134 Fabled Rooks

    解题报告 之 UVA11134 Fabled Rooks Description Problem F: Fabled Rooks We would like to place n rooks, 1 ≤ ...

  3. UVA11134 Fabled Rooks

    摘要: 贪心 链接 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  4. uva11134 -Fabled Rooks

    题意: 一个n*n的矩阵上放n个车,第i辆车在第i个区间上,每个区间给出左上角和右下角的坐标.任意两个车之间同行同列不能互相攻击,求这些车放置 的坐标. 思路: 第一眼看以为是N皇后的变形,后来发现车 ...

  5. 11134 - Fabled Rooks

    Fabled Rooks We would like to place nn rooks, 1≤n≤50001 \le n \le 5000, on a n×nn \times n board sub ...

  6. 8-4 Fabled Rooks uva11134

    题意:你的任务是在n*n的棋盘上放 n 小于5000 个车 使得任意两个车不互相攻击 且第i个车在一个给定的矩形ri之内  给出该矩形左上角坐标和右下角坐标四个点  必须满足放车的位置在矩形内  边上 ...

  7. UVA11134 传说中的车 Fabled Rooks

    首先,根据数据范围,可以得到这是一题O(N2) 考虑贪心 发现行和列是不相关的,于是可以把他们分成两个一维区间问题,也就是在线段中选出点使得每个线段中都有一个点,求出方案. 先考虑尽量不对后面造成影响 ...

  8. UVA 11134 - Fabled Rooks(经典贪心)

    题目链接 https://cn.vjudge.net/problem/UVA-11134 [题意] 你的任务是在n×n的棋盘上放置n辆车,使得任意两辆车不互相攻击,且第i辆车在一个给定的矩形Ri以内. ...

  9. uva 11134——Fabled Rooks

    题意:给定一个n*n的期棋盘放n个车,要求任意车之间不能相互攻击,并且每个车都在相应的方框内. 思路:贪心.因为没有对角线的条件约束,所以放的行号和列号没有影响.那么单独求出来行号和列号即可.对于每一 ...

最新文章

  1. 让你的网站提速:图片优化网站推荐
  2. 面试官让我手写一个生产者消费者模式?
  3. 纠缠不清的C语言位域(位段)详解
  4. 验证码识别Burp reCAPTCHA插件使用
  5. 分布式系统:CAP 理论的前世今生
  6. python解释器有多大_python解释器到底是什么?
  7. 从0使用webpack构建reactjs
  8. 在idea中使用debug
  9. Mac m1 max 工具安装及简介
  10. requirejs加载layerdate.js遇到的各种坑
  11. 一加手机怎么root权限_一加手机怎么解除root权限
  12. 《菜鸟教程》C语言学习
  13. maya表情blendshape_【UE4】人物角色MorphTarget(Blendshape) 面部表情制作方案
  14. 微信小程序开发问题收集及解决方案——样式篇
  15. 目前比较流行的网站设计风格有哪些
  16. 9820E ClassicHome分析总结
  17. Jodd HTTP的使用
  18. 面试——如何测试一支笔?
  19. MySQL学习的第一天 查询
  20. 网络命令以及如何使用Ipconfig.exe工具查看TCP/IP配置

热门文章

  1. 以过来人经验---分享从学生--工程师之--怎么写好一份从事技术工作的简历及面试技巧(以嵌入式为例)(中)
  2. vue 使用fetch 出现问题解决以及 相应知识学习
  3. ⼤数据平台基础架构及解决⽅案
  4. 计算机系统——信息的表示与处理
  5. python使用xlwt模块操作Excel
  6. win10自启动方法
  7. Windows Moblie上的网络连接
  8. 硬实时RTLinux安装配置详解 (一):准备工作
  9. 输入现在的日期,输出明天的日期
  10. LSB隐写(最低有效位隐写)