题目大意

  有一些不相交线段和一些桥,桥可以架在两个相邻的线段上。求现有的桥是否可以使所有线段连通。

题解

  在两个线段上架桥,桥的长度在一个范围内,相当于一个长度的区间,一个桥只有一个长度,相当于一个长度的。这就转化成了点匹配区间问题。

  点匹配区间问题我们在贪心(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的更多相关文章

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

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

  3. Codeforces 556D - Case of Fugitive

    556D - Case of Fugitive 思路:将桥长度放进二叉搜索树中(multiset),相邻两岛距离按上限排序,然后二分查找桥长度匹配并删除. 代码: #include<bits/s ...

  4. Codeforces 555 B. Case of Fugitive

    \(>Codeforces \space 555 B. Case of Fugitive<\) 题目大意 : 有 \(n\) 个岛屿有序排列在一条线上,第 \(i\) 个岛屿的左端点为 \ ...

  5. CodeForces - 556D Case of Fugitive (贪心+排序)

    Andrewid the Android is a galaxy-famous detective. He is now chasing a criminal hiding on the planet ...

  6. Codeforces555 B. Case of Fugitive

    Codeforces题号:#310B 出处: Codeforces 主要算法:贪心+优先队列 难度:4.6 思路分析: 这道题乍一看没有思路…… 考虑贪心的做法.首先预处理出每两座相邻的桥之间边界相差 ...

  7. codeforces 555b//Case of Fugitive// Codeforces Round #310(Div. 1)

    题意:有n-1个缝隙,在上面搭桥,每个缝隙有个ll,rr值,ll<=长度<=rr的才能搭上去.求一种搭桥组合. 经典问题,应列入acm必背300题中.属于那种不可能自己想得出来的题.将二元 ...

  8. codeforces 555B Case of Fugitive

    题目连接: http://codeforces.com/problemset/problem/555/B 题目大意: 有n个岛屿(岛屿在一列上,可以看做是线性的,用来描述岛屿位置的是起点与终点),m个 ...

  9. CF555B 贪心

    http://codeforces.com/problemset/problem/555/B B. Case of Fugitive time limit per test 3 seconds mem ...

随机推荐

  1. [ BZOJ 3038 & 3211 / SPOJ GSS4 ] 上帝造题七分钟2 / 花神游历各国

    \(\\\) \(Description\) 给出一个长度为\(N\)的数列,共进行\(M\)次操作: \(1\ L\ R\):查询\([L,R]\)区间和. \(2\ L\ R\):对\([L,R] ...

  2. python框架之Flask基础篇(三)-------- 模版的操作

    1.flask特有的变量和函数: 变量:g.session.request.config 函数:url_for().get_flashed_messages()这个函数注意了啊,记住这是个函数,别忘了 ...

  3. iOS 从xib中加载自定义视图

    想当初在学校主攻的是.NET,来到公司后,立马变成java开发,之后又跳到iOS开发,IT人这样真的好么~~  天有不测风云,云还有变幻莫测哎,废话Over,let's go~ 新学iOS开发不久,一 ...

  4. TensorFlow学习---入门(一)-----MNIST机器学习

    参考教程:http://www.tensorfly.cn/tfdoc/tutorials/mnist_beginners.html 数据下载地址:http://wiki.jikexueyuan.com ...

  5. MyEclipse快捷键 (精简)

    在调试程序的时候,我们经常需要注释一些代码,在用Myeclipse编程时,就可以用 Ctrl+/ 为选中的一段代码加上以 // 打头的注释:当需要恢复代码功能的时候,又可以用Ctrl+/ 去掉注释.这 ...

  6. 正文处理命令及tar命令

    使用cat命令进行文件的纵向合并,具体命令如下所示(注意:>代表将左边命令的执行结果以覆盖的方式放到右边,>>代表将左边命令的执行结果追加到右边) 关于tar命令的一些用法: tar ...

  7. vsftpd:500OOPS:vsftpd:refusingtorunwithwritablerootinsidechroot()错误的解决方法

    当我们限定了用户不能跳出其主目录之后,使用该用户登录FTP时往往会遇到这个错误: 500 OOPS: vsftpd: refusing to run with writable root inside ...

  8. @Override注解

    @Override注解对于代码可读性的提升十分巨大 而且良好的可读性是一个优秀程序员必备的基础素养

  9. bootstrap table分页(前后端两种方式实现)

    bootstrap table分页的两种方式: 前端分页:一次性从数据库查询所有的数据,在前端进行分页(数据量小的时候或者逻辑处理不复杂的话可以使用前端分页) 服务器分页:每次只查询当前页面加载所需要 ...

  10. swift-延时加载函数

    //延时加载函数 func delayLoad(){ let time: NSTimeInterval = 2.0 let delay = dispatch_time(DISPATCH_TIME_NO ...