CF555B Case of Fugitive
题目大意
有一些不相交线段和一些桥,桥可以架在两个相邻的线段上。求现有的桥是否可以使所有线段连通。
题解
在两个线段上架桥,桥的长度在一个范围内,相当于一个长度的区间,一个桥只有一个长度,相当于一个长度的点。这就转化成了点匹配区间问题。
点匹配区间问题我们在贪心(POJ3614)那里学了,把所有区间按照左端点从大到小排序,把点按照位置从大到小排序,每次总是把最右侧区间与在该区间内的最右端点匹配。问题是:如何满足可以随时删除点,且可以快速找到该区间内的最右端点呢?用key值为点的长度递减的multiset的delete和lower_bound函数可以轻松解决。注意delete时传的参数时迭代器(指针),而不是值,否则一删就把重合的点全删了。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
using namespace std; const int MAX_LAND = 200010, MAX_BRIDGE = 200010;
int TotLand, TotLenRange, TotBridge;
int Ans[MAX_BRIDGE]; struct Bridge
{
long long Len;
int OrgP; bool operator < (const Bridge& a) const
{
return Len > a.Len;
}
}_bridges[MAX_BRIDGE]; struct Cmp
{
bool operator () (const Bridge& a, const Bridge& b)
{
return a.Len < b.Len;
}
}; struct Land
{
long long L, R; bool operator < (const Land& a) const
{
return L < a.L;
}
}_lands[MAX_LAND]; struct LenRange
{
long long L, R;
int OrgL; bool operator < (const LenRange& a) const
{
return L > a.L;
}
}_lenRanges[MAX_LAND]; void Read()
{
scanf("%d%d", &TotLand, &TotBridge);
TotLenRange = TotLand - 1;
for (int i = 1; i <= TotLand; i++)
scanf("%lld%lld", &_lands[i].L, &_lands[i].R);
for (int i = 1; i <= TotBridge; i++)
scanf("%lld", &_bridges[i].Len);
} void Init()
{
for (int i = 1; i <= TotBridge; i++)
_bridges[i].OrgP = i;
sort(_lands + 1, _lands + TotLand + 1);
for (int i = 1; i <= TotLand - 1; i++)
{
_lenRanges[i].L = _lands[i + 1].L - _lands[i].R;
_lenRanges[i].R = _lands[i + 1].R - _lands[i].L;
_lenRanges[i].OrgL = i;
}
sort(_lenRanges + 1, _lenRanges + TotLenRange + 1);
} void Solve()
{
static multiset<Bridge> tree;
for (int i = 1; i <= TotBridge; i++)
tree.insert(_bridges[i]);
for (int i = 1; i <= TotLenRange; i++)
{
Bridge temp;
temp.Len = _lenRanges[i].R;
multiset<Bridge>::iterator it = tree.lower_bound(temp);
if (it == tree.end() || it->Len <_lenRanges[i].L )
{
printf("No\n");
return;
}
Ans[_lenRanges[i].OrgL] = it->OrgP;
tree.erase(it);
}
printf("Yes\n");
for (int i = 1; i <= TotLand - 1; i++)
printf("%d ", Ans[i]);
printf("\n");
} int main()
{
Read();
Init();
Solve();
return 0;
}
CF555B Case of Fugitive的更多相关文章
- Codeforces Round #310 (Div. 1) B. Case of Fugitive set
B. Case of Fugitive Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/555/p ...
- Codeforces Round #310 (Div. 1) B. Case of Fugitive(set二分)
B. Case of Fugitive time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 556D - Case of Fugitive
556D - Case of Fugitive 思路:将桥长度放进二叉搜索树中(multiset),相邻两岛距离按上限排序,然后二分查找桥长度匹配并删除. 代码: #include<bits/s ...
- Codeforces 555 B. Case of Fugitive
\(>Codeforces \space 555 B. Case of Fugitive<\) 题目大意 : 有 \(n\) 个岛屿有序排列在一条线上,第 \(i\) 个岛屿的左端点为 \ ...
- CodeForces - 556D Case of Fugitive (贪心+排序)
Andrewid the Android is a galaxy-famous detective. He is now chasing a criminal hiding on the planet ...
- Codeforces555 B. Case of Fugitive
Codeforces题号:#310B 出处: Codeforces 主要算法:贪心+优先队列 难度:4.6 思路分析: 这道题乍一看没有思路…… 考虑贪心的做法.首先预处理出每两座相邻的桥之间边界相差 ...
- codeforces 555b//Case of Fugitive// Codeforces Round #310(Div. 1)
题意:有n-1个缝隙,在上面搭桥,每个缝隙有个ll,rr值,ll<=长度<=rr的才能搭上去.求一种搭桥组合. 经典问题,应列入acm必背300题中.属于那种不可能自己想得出来的题.将二元 ...
- codeforces 555B Case of Fugitive
题目连接: http://codeforces.com/problemset/problem/555/B 题目大意: 有n个岛屿(岛屿在一列上,可以看做是线性的,用来描述岛屿位置的是起点与终点),m个 ...
- CF555B 贪心
http://codeforces.com/problemset/problem/555/B B. Case of Fugitive time limit per test 3 seconds mem ...
随机推荐
- PHP流程控制语句(if,foreach,break......)
背景:PHP程序中,必不可少的要用到流程控制语句.这次对于流程控制语句进行一些总结. 条件控制语句和循环控制语句是两种基本的语法结构,它们都是用来控制程序执行流程.也是构成程序的主要语法基础. 一.程 ...
- sublime text3配置插件
之前一直习惯用记事本写代码,懒得用IDE,虽然知道用 IDE效率高一些,不过觉得还是用记事本纯手写代码,比较容易记忆.直到昨天写代码遇到了点问题,截图给师兄看,师兄就问我是不是用记事本写代码,为什么不 ...
- Hibernate中使用子查询
子查询: 子查询是SQL语句中非常重要的功能特性,它可以在SQL语句中利用另外一条SQL语句的查询结果,在Hibernate中HQL查询同样对子查询功能提供了支持. 如下面代码所示: List ...
- SQl基本操作——try catch
begin try ... end try begin catch ... end catch
- 10、scala面向对象编程之Trait
1. 将trait作为接口使用 2.trait中定义具体方法 3.trait定义具体字段 4.trait中定义抽象字段 5.为实例对象混入trait 6.trait调用链 7.在trait中覆盖抽象 ...
- haproxy故障处理
1. haproxy 在配置健康检查的时候,默认没有配置页面检查 ,通过端口状态来检测.后端IIS web服务开始可能 是一个站点,或者采用了基于域名的配置方式,导致目前站点停了,后端主机不能被hap ...
- Html5 WebSocket详细介绍
什么是WebSocket?看过html5的同学都知道,WebSocket protocol 是HTML5一种新的协议.它是实现了浏览器与服务器全双工通信(full-duplex).HTML5定义了We ...
- select 多选 和单选,分组
<select name="group"> <option value="1">北京</option> <option ...
- POJ3278——Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 114140 Accepted: 35715 ...
- bat 读取当前目录指定文件信息并拼接
bat 读取指定文件的信息并拼接成指定格式