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 ...
随机推荐
- Python之NumPy实践之数组和矢量计算
Python之NumPy实践之数组和矢量计算 1. NumPy(Numerical Python)是高性能科学技术和数据分析的基础包. 2. NumPy的ndarray:一种对位数组对象.NumPy最 ...
- Ubuntu16安装jdk8配置Tomcat9
一.配置jdk 1.下载解压是肯定不能少的 2.配置环境变量根据自己需求来 export JAVA_HOME=/usr/software/jdk1.8.0_121 export CLASSPATH=. ...
- postgresql用sql语句查询表结构
用到的postgresql系统表 关于postgresql系统表,可以参考PostgreSQL 8.1 中文文档-系统表. pg_class 记录了数据库中的表,索引,序列,视图("关系&q ...
- Windows Server 2008无法远程连接
Server 2008 R2依次配置好之后,重启发现总是远程桌面时而连接不上.具体现象如下: 偶尔可以通过桌面远程连接连接到Server.以为是防火墙的问题,各种设置——甚至关闭,依然无法连接.反复重 ...
- Scroll / Jump to id without jQuery
<scripttype="text/javascript"> function scroll(element){var ele = document.getElemen ...
- tidyverse生态链
一套完整的数据分析流程 , 如下图所示 从图中可以看到,整个流程包括读取数据,整洁数据,数据探索和交流部分.经过前两部分, 我们可以得到一个整理好的数据,它的每一行都是一个样本 , 每一列是一个变量. ...
- JAVA学习总结-常用数据结构
java中集合框架其实就是数据结构的实现的封装; 参考资料:任小龙教学视频 1,什么是数据结构? 数据结构是计算机存储,组织数据的方式; 数据结构是指相互之间存在一种或多种特定关系的数据元素的集合; ...
- C++关键字:重学记录
const_cast dynamic_cast explicit
- Scrapy Item用法示例(保存item到MySQL数据库,MongoDB数据库,使用官方组件下载图片)
需要学习的地方: 保存item到MySQL数据库,MongoDB数据库,下载图片 1.爬虫文件images.py # -*- coding: utf-8 -*- from scrapy import ...
- Flask - 内置Session
目录 Flask - 内置Session 基本用法 给视图添加装饰器验证 Flask - 内置Session Flask中的Session非常的奇怪,他会将你的SessionID存放在客户端的Cook ...