题目链接: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(问题转换+优先队列)的更多相关文章

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

  2. UVA 11134 Fabled Rooks 贪心

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

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

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

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

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

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

  6. UVA 11134 Fabled Rooks

    贪心+优先队列+问题分解 对x,y 分开处理 当 xl<cnt(当前处理行)时,不能简单的选择cnt,而是应该让xl=cnt 并重新加入优先队列.(y的处理同上) #include <io ...

  7. UVa 11134 - Fabled Rooks——[问题分解、贪心法]

    We would like to place n rooks, ≤ n ≤ , on a n × n board subject to the following restrictions • The ...

  8. UVa 11134 Fabled Rooks(贪心)

    题目链接  题意  在n*n的棋盘上的n个指定区间上各放1个'车’ , 使他们相互不攻击(不在同行或同列),输出一种可能的方法. 分析 每行每列都必须放车,把行列分开看,若行和列同时有解,则问题有解. ...

  9. UVA 11134 Fabled Rooks(贪心的妙用+memset误用警示)

    题目链接: https://cn.vjudge.net/problem/UVA-11134 /* 问题 输入棋盘的规模和车的数量n(1=<n<=5000),接着输入n辆车的所能在的矩阵的范 ...

随机推荐

  1. [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(九)

    目的 1. 使用verilog/vhdl设计一个PCI的watchdog设备. 2. 通过systemverilog 写testbench. 很久之前研究过AC97的verilog代码.但是很久没用v ...

  2. ldap理论属于概念缩略词

    Standalone LDAP Daemon, slapd(standalone lightweight access protocol) ldap 389 default listener port ...

  3. 面向对象程序设计-C++_课时19const_课时20不可修改的

    error C2131: 表达式的计算结果不是常数 #include <iostream> using namespace std; void main() { ; int finalGr ...

  4. mac 版本号控制工具SmartSVN7.5.4(破解版)

    SmartSVN7.5.4和破解工具,下载地址:http://download.csdn.net/detail/pearlhuzhu/7407319 操作步骤: 1.在MAC上选中smartsvn-m ...

  5. c# 小数的处理

    数值类型处理小数 1.Math.Round(x) 四舍五入      Math.Round(0.4) 0     Math.Round(-1.7) -2 2.Math.floor(x) 小于等于 x, ...

  6. Struts学习之自定义拦截器

    * 所有的拦截器都需要实现Interceptor接口或者继承Interceptor接口的扩展实现类    * 要重写init().intercept().destroy()方法        * in ...

  7. (4)事件处理——(1)事件处理(Handling Events)

    JavaScript has several built-in ways of reacting to user interaction and other events. To make a pag ...

  8. easyui 验证控件 tooltip message显示位置

    找了半天才发现是这个属性在控制,tipPosition:'left',官网那个demo,误人子弟.

  9. PHP中include和require的区别

    include和require的区别,其实两者没有太大的区别,如果要包含的文件不存在,include提示notice,然后继续执行下面的语句,require提示致命错误并且退出. 根据测试,win32 ...

  10. check the manual that corresponds to your MySQL server version for the right syntax的错误解析

    错误原因一:SQL关键字冲突 分析:例:把desc命名为字段名 错误原因二:$right=$DB->fetch_one_array("SELECT rsnumber FROM &quo ...