uva 11134 - Fabled Rooks(问题转换+优先队列)
题目大意:给出n,表示要在n*n的矩阵上放置n个车,并且保证第i辆车在第i个区间上,每个区间给出左上角和右小角的坐标。另要求任意两个车之间不能互相攻击。
解题思路:因为要保证说每两个车之间不能互相攻击,那么即任意行列都不能摆放两个以上的车,转而言之可以看成是将每一行或列分配给每辆车。如果行和列和起来考虑的话复杂度太高了,但是行和列的分配又互相不影响,所以可以分开讨论。
即对于一个区间[xl,xr],要分配一个x给它,做法和uva 1422一样。
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm> using namespace std; const int N = 5005; struct state {
int l, r, id;
friend bool operator < (const state a, const state b) {
return a.r > b.r;
}
}x[N], y[N], ans[N];
int n; bool cmp(const state& a, const state& b) {
return a.l < b.l;
} void init() {
for (int i = 0; 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);
} bool solve() {
priority_queue<state> q;
state c; int p = 0;
for (int i = 0; i < n; i++) {
while(p < n) {
if (x[p].l <= i + 1) q.push(x[p]);
else break;
p++;
} if (q.empty()) return false; c = q.top(); q.pop();
if (c.r < i + 1) return false;
ans[c.id].l = i + 1;
} p = 0;
for (int i = 0; i < n; i++) {
while(p < n) {
if (y[p].l <= i + 1) q.push(y[p]);
else break;
p++;
} if (q.empty()) return false; c = q.top(); q.pop();
if (c.r < i + 1) return false;
ans[c.id].r = i + 1;
} for (int i = 0; i < n; i++) printf("%d %d\n", ans[i].l, ans[i].r);
return true;
} int main () {
while (scanf("%d", &n) == 1 && n) {
init();
if (solve() == false) printf("IMPOSSIBLE\n");
}
return 0;
}
uva 11134 - Fabled Rooks(问题转换+优先队列)的更多相关文章
- 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 题意描述:在一个n*n(1<=n<=5000)的棋盘上放置n个车,每个车都只能在给定的一个矩形里放置,使其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 ...
- UVa 11134 - Fabled Rooks 优先队列,贪心 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- uva 11134 fabled rooks (贪心)——yhx
We would like to place n rooks, 1 n 5000, on a n nboard subject to the following restrictions• The i ...
- UVA 11134 Fabled Rooks
贪心+优先队列+问题分解 对x,y 分开处理 当 xl<cnt(当前处理行)时,不能简单的选择cnt,而是应该让xl=cnt 并重新加入优先队列.(y的处理同上) #include <io ...
- UVa 11134 - Fabled Rooks——[问题分解、贪心法]
We would like to place n rooks, ≤ n ≤ , on a n × n board subject to the following restrictions • The ...
- UVa 11134 Fabled Rooks(贪心)
题目链接 题意 在n*n的棋盘上的n个指定区间上各放1个'车’ , 使他们相互不攻击(不在同行或同列),输出一种可能的方法. 分析 每行每列都必须放车,把行列分开看,若行和列同时有解,则问题有解. ...
- UVA 11134 Fabled Rooks(贪心的妙用+memset误用警示)
题目链接: https://cn.vjudge.net/problem/UVA-11134 /* 问题 输入棋盘的规模和车的数量n(1=<n<=5000),接着输入n辆车的所能在的矩阵的范 ...
随机推荐
- Spring AOP AspectJ Pointcut 表达式例子
主要来源:http://howtodoinjava.com/spring/spring-aop/writing-spring-aop-aspectj-pointcut-expressions-with ...
- UberX及以上级别车奖励政策(优步北京第一组)
优步北京第一组: 定义为2015年6月1日凌晨前(不含6月1日)激活的司机(以优步后台数据显示为准) 滴滴快车单单2.5倍,注册地址:http://www.udache.com/如何注册Uber司机( ...
- Android_Intent意图详解
本博文为子墨原创,转载请注明出处! http://blog.csdn.net/zimo2013/article/details/11863857 1.Intent作用 Intent是一个将要执行的动作 ...
- SSH整合方案2
[案例3]SSH整合_方案2 ** 案例描述 两个知识点的演示 其一,SSH整合的第二个方案 其二,Spring+JDBC+Struts2 参考代码 31) 使用工程spring4 32 ...
- 从零开始Unity3D游戏开发【2 简单的水管工例子】
1.首先,创建一个新的Project. 2.hierarchy(层)窗体下的Create下添加一个plane(平面) 3.调整Main Camera的视角,让panel显示在Game窗体.这一步比较困 ...
- 从一句SQL得出的启示
select count(*) + 1 from `table` where rank > (select rank from `table` where id = *) 上面那句SQL 给了我 ...
- 在centos6.5下yum仓库的创建
第一步:打开虚拟机,装入光盘镜像,选择为已连接 第二步: df -h mount umount /dev/sr0 mkdir /centos mount /dev/sr0 /centos mkdir ...
- As Easy As A+B
Problem Description These days, I am thinking about a question, how can I get a problem as easy as A ...
- apache rewrite rule
http://10.58.104.19:8008/site/833/3f11d2b44b7d3baa2149f26a30f8c68d/b.js?siteid=332323 将一个静态请求转换成一个动态 ...
- Android常用工具类封装---SharedPreferencesUtil
SharedPreferences常用于保存一些简单的数据,如记录用户操作的配置等,使用简单. public class SharedPreferencesUtil { // ...