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 ...
随机推荐
- JS——if条件判断
现在只说特殊情况: 1.一个变量,例如n1=null <script> var n1 = null; alert(n1);/*弹窗的值为null*/ if (n1 == null) {/* ...
- IE bug集锦
ie8 iframe 不显示 问题描述: IE8的非兼容模式下(兼容模式是ie7,不存在),iframe会不显示: 可以通过Ctrl+A全选或者是调整窗口大小显示出来. 解决办法: 这是由于要显示的i ...
- iOS crash log 解析 symbol address = stack address - slide 运行时获取slide的api 利用dwarfdump从dsym文件中得到symbol
概述: 为什么 crash log 内 Exception Backtrace 部分的地址(stack address)不能从 dsym 文件中查出对应的代码? 因为 ASLR(Address spa ...
- 前端精选文摘:css之GFC 神奇背后的原理(整理)
CSS3 Grid Layout Web页面的布局,我们常见的主要有“浮动布局(float)”.“定位布局(position)”.“行内块布局(inline-block)”.“CSS3的多栏布局(Co ...
- Java数值数学类
Java数值数学类 序号 方法与描述 1 xxxValue() 将 Number 对象转换为xxx数据类型的值并返回. 2 compareTo() 将number对象与参数比较. 3 equals ...
- mt_rand()和rand()两者的区别
在随机读取中使用了mt_rand(),而不适用rand(),他们两者的区别: mt_rand()是更好地随机数生成器,因为它跟rand()相比播下了一个更好地随机数种子:而且性能上比rand()快4倍 ...
- Laravel实用小功能
Laravel实用小功能 1.控制访问次数 laravel5.2的新特性,通过中间件设置throttle根据IP控制访问次数 原理:通过回传三个响应头X-RateLimit-Limit,X-RateL ...
- 内存管理(malloc和free的用法)
内存管理 1.堆和栈的区别: 1>栈的特征 1).执行的速度相对较快: 2).空间较小: 3).生存期由系统决定: 4).作用域较小: 5).有名空间,可以通过变量名或者数据名访问: 2> ...
- Navicat premium连接Oracle报ORA-12514错误
1:ORA-12514 原因:Service Name/SID中的值填的有问题,默认的是ORCL,这个值如果在安装Oracle的时候填的不是ORCL,那就会出现这个错误 解决方法:找到Oracle安装 ...
- Java Web学习总结(23)——Distributed Configuration Management Platform(分布式配置管理平台)
专注于各种 分布式系统配置管理 的通用组件/通用平台, 提供统一的配置管理服务. 主要目标: 部署极其简单:同一个上线包,无须改动配置,即可在 多个环境中(RD/QA/PRODUCTION) 上线 部 ...