题目链接: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. 指定html内容下载为word文档

    解决思路是:获取html内容并传到后台,后台把html内容转换为输入流再传给浏览器,浏览器直接下载 1.获取html内容并传到后台 $("#zxjdck .ad-xzzy-anniu&quo ...

  2. windows 7 memcached报failed to install service or service already installed的解决方案

    今天心血来潮捣鼓一下memcache,由于系统是windows 7,我参考了 Windows下安装Memcache 使用memcached for Win32. 在运行memcached.exe -d ...

  3. POJ 3831 &amp; HDU 3264 Open-air shopping malls(几何)

    题目链接: POJ:id=3831" target="_blank">http://poj.org/problem?id=3831 HDU:http://acm.h ...

  4. kettle中调用java类

    kettle中调用java类 有时须要在kettle调用java类,如:验证.查询或自己定义加密等.有时甚至连主要的数据訪问都不那么简单,如获取一个存储文件或使用一个数据库连接,某些数据源可能封装在应 ...

  5. iOS UISearchBar学习笔记

    UISearchBar 是一个搜索控件,它提供了一个文本输入框,一个查找button,一个书签button.一个取消button.我们须要使用UISearchBarDelegate代理来进行查找工作. ...

  6. SQL语句一些特殊的用法

    SQL语句一些特殊的用法 一.基础 1.说明:创建数据库 CREATE DATABASE database-name  2.说明:删除数据库 drop database dbname 3.说明:备份s ...

  7. IIS7.5(IIS7)配置伪静态urlrewrite

    找了好久,终于找到了.已经测试通过,收藏. 转载自:http://jingyan.baidu.com/article/67508eb4ff92c69cca1ce49a.html 首先新建一个应用程序池 ...

  8. android入门——UI(1)

    一.使用TextView ImageView Button EditView做出登录页面 <?xml version="1.0" encoding="utf-8&q ...

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

  10. hadoop搭建杂记:Linux下ssh免密码登陆

    关于ssh免密码登陆的问题 关于ssh免密码登陆的问题 linux下可以用ssh-keygen来生成公钥/私钥对 ①生成id_rsa和id_rsa.pub公钥/私钥对,自动在~/.ssh下生成文件(亦 ...