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辆车的所能在的矩阵的范 ...
随机推荐
- javascript关键字加亮加连接
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" " http://www.w3.org/TR/html4/str ...
- C++四种强制类型转换详解
什么是类型转换? 类型转换的含义是通过改变一个变量的类型为别的类型从而改变该变量的表示方式.为了类型转换一个简单对象为另一个对象你会使用传统的类型转换操作符. C与C++的类型转换 //C中: //复 ...
- Class constructor
// example: class constructor #include <iostream> using namespace std; class Rectangle { in ...
- ASP.NET MVC4 ASP.NET Web API路由规则
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Web.Ht ...
- ASPxGridview在对话框中无法编辑!!
aspxgridview在使用window.showModelDialog(或者window.showModelessDialog)打开的窗体中居然无法进入编辑!好奇怪啊 . 点击后显示“无法显示网 ...
- bootstrap2.3.2常用标签的使用
<!DOCTYPE html> <html lang="zh_CN"> <head> <title>Bootstrap 101 Te ...
- hosts文件简析
什么是hosts文件 hosts文件是个什么文件呢?Hosts虽然没有后缀名,其实是个纯文本文件,可以用记事本等文本编辑软件打开.Hosts文件主要用于在本地电脑强制解析域名,Hosts文件里包含映射 ...
- BZOJ 4197: [Noi2015]寿司晚宴( dp )
N^0.5以内的质数只有8个, dp(i, j, k)表示用了前i个大质数(>N^0.5), 2人选的质数(<=N^0.5)集合分别为j, k时的方案数. 转移时考虑当前的大质数p是给哪个 ...
- PHPEXCEL导入小技巧
在导入excel的时候,单元格格式和公式经常让导入不顺畅.注意phpexcel文档说明,基本上就可以很顺利的导入. 1.忽略单元格格格式,并导入xls.xlsx两种格式 $objReader = PH ...
- R与数据分析旧笔记(十一)数据挖掘初步
PART 1 PART 1 传统回归模型的困难 1.为什么一定是线性的?或某种非线性模型? 2.过分依赖于分析者的经验 3.对于非连续的离散数据难以处理 网格方法 <Science>上的文 ...