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 ...
随机推荐
- 【译】x86程序员手册15-5.2页转换
5.2 Page Translation 页转换 In the second phase of address transformation, the 80386 transforms a linea ...
- 3星|《管理十诫》:十年前可口可乐退休CEO的一生管理经验总结
管理十诫:影响你一生的管理哲学 英文书应该是2008年出版的.国内出版过几个译本. 作者是可口可乐CEO.本书是他从可口可乐CEO退下来后写的管理经验总结.作者总结了11条CEO不应该做的事.这11条 ...
- RAID技术简单分析
RAID技术解析 RAID:独立磁盘冗余阵列(Redundant Array of Independent Disks) RAID技术就是将许多块硬盘设备组合成一个容量更大.更安全的硬盘组,可以将数据 ...
- 【转载】HTTP 缓存的四种风味与缓存策略
原文地址:https://segmentfault.com/a/1190000006689795 HTTP Cache 通过网络获取内容既缓慢,成本又高:大的响应需要在客户端和服务器之间进行多次往返通 ...
- PHP 之中文转为拼音
/** * Created by PhpStorm. * User: Administrator * Date: 2019/1/2 0002 * Time: 下午 1:01 */ class PinY ...
- cstringlist
CStringList类成员 构造 CStringList 构造一个空的CString对象列表 首/尾访问 GetHead 返回此列表(不能是空的)中头部的元素 GetTail 返回此列表(不能是 ...
- PHP常用的设计模式
工厂模式 工厂模式是我们最常用的实例化对象模式,是用工厂方法代替new操作的一种模式. 使用工厂模式的好处是如果你想要更改所实例化的类名等,则只需要更改该工厂方法内容即可,不需逐一寻找代码中具体实例化 ...
- 洛谷 2042 BZOJ 1500 NOI 2005 维护数列
[题意概述] 维护一个数列,要求支持以下6种操作: [题解] 大Boss...可以用Treap解决 需要用到垃圾回收.线性建树. #include<cstdio> #include< ...
- BZOJ 1634 洛谷2878 USACO 2007.Jan Protecting the flowers护花
[题意] 约翰留下他的N只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时候,他看到了一幕惨剧:牛们正躲在他的花园里,啃食着他心爱的美丽花朵!为了使接下来花朵的损失最小 ...
- hdu 3500 DFS(限定)
Fling Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submi ...