Codeforces 555 B. Case of Fugitive
\(>Codeforces \space 555 B. Case of Fugitive<\)
题目大意 : 有 \(n\) 个岛屿有序排列在一条线上,第 \(i\) 个岛屿的左端点为 \(l_i\) 右端点为 \(r_i\) ,岛屿之间两两不相交, 现在对于每一个 \(1 \leq i < n\) 第 \(i\) 岛屿要和第 \(i + 1\) 岛屿之间建一座桥,桥的长度左右端点必须得在岛上。现在有 \(m\) 座已经长度建好的桥梁,试找出一种岛屿和桥匹配的方案,使得任意两座岛屿之间的桥梁长度都满足要求
\(2 ≤ n, m ≤ 2 \times 10^5\ 1 ≤ l_i ≤ r_i ≤ 10^{18}\)
解题思路 :
问题可以转化为 \(n-1\) 条线段匹配 \(m\) 个点,使得点在线段之内,找出一种匹配完所有线段的方案
有一种显然的贪心策略,排完序后对于每一个点尽可能选右端点小的线段
可以感性理解,因为点是递增的,右端点越小的线段越往后越不可能有匹配
考虑将所有线段和点按照左端点排序, 从左到右枚举每一个点为其找线段匹配
维护一个优先队列来存线段,每枚举到一个点就将所有左端点小于它的线段加进优先队列
对于每一个点取优先队列中 \(r_i\) 最小的进行匹配,如果发现某一时刻最小的 \(r_i <\) 当前的点
那么对于之后的所有点,这个线段都无法被匹配了,必然是无解,否则就匹配完输出方案
/*program by mangoyang*/
#include<bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
int f = 0, ch = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if(f) x = -x;
}
#define int ll
#define N (500005)
int l[N], r[N], Ans[N], n, m;
struct Node{ int x, id; } a[N];
struct Seg{
int l, r, id;
bool operator < (const Seg &A) const{ return r > A.r; }
}s[N];
inline bool cmp(Seg A, Seg B){ return A.l < B.l; }
inline bool cmp2(Node A, Node B){ return A.x < B.x; }
priority_queue<Seg> pq;
main(){
read(n), read(m);
if(m < n - 1) return puts("No"), 0;
for(int i = 1; i <= n; i++) read(l[i]), read(r[i]);
for(int i = 1; i <= m; i++) read(a[i].x), a[i].id = i;
for(int i = 1; i < n; i++)
s[i] = (Seg){l[i+1] - r[i], r[i+1] - l[i], i};
sort(s + 1, s + n, cmp);
sort(a + 1, a + m + 1, cmp2);
int p = 1;
for(int i = 1; i <= m; i++){
while(a[i].x >= s[p].l && p < n) pq.push(s[p]), p++;
if(pq.empty()) continue;
if(pq.top().r < a[i].x) return puts("No"), 0;
Seg now = pq.top(); pq.pop(); Ans[now.id] = a[i].id;
}
for(int i = 1; i < n; i++) if(!Ans[i]) return puts("No"), 0;
puts("Yes");
for(int i = 1; i < n; i++) printf("%lld ", Ans[i]);
return 0;
}
Codeforces 555 B. Case of Fugitive的更多相关文章
- Codeforces 555 C. Case of Chocolate
\(>Codeforces \space 555 C. Case of Chocolate<\) 题目大意 : 有一块 \(n \times n\) 的倒三角的巧克力,有一个人要吃 \(q ...
- 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 555B Case of Fugitive
题目连接: http://codeforces.com/problemset/problem/555/B 题目大意: 有n个岛屿(岛屿在一列上,可以看做是线性的,用来描述岛屿位置的是起点与终点),m个 ...
- CodeForces - 556D Case of Fugitive (贪心+排序)
Andrewid the Android is a galaxy-famous detective. He is now chasing a criminal hiding on the planet ...
- codeforces 555b//Case of Fugitive// Codeforces Round #310(Div. 1)
题意:有n-1个缝隙,在上面搭桥,每个缝隙有个ll,rr值,ll<=长度<=rr的才能搭上去.求一种搭桥组合. 经典问题,应列入acm必背300题中.属于那种不可能自己想得出来的题.将二元 ...
- 【35.37%】【codeforces 556C】Case of Matryoshkas
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【66.47%】【codeforces 556B】Case of Fake Numbers
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- div圆角
div{ -moz-border-radius: 10px; -webkit-border-radius: 10px; border-radius: 10px;}
- docker使用小记
查看当前镜像:docker images 运行一个简单的镜像:docker run hello-world 拉取一个远程docker:docker pull centos docker中安装nginx ...
- Django之项目搭建和配置总结(一)
安装和创建虚拟环境 参考:linux系统下Python虚拟环境的安装和使用 安装Django包 先进入虚拟环境,在联网下执行: pip install django==1.8.7 1.8.7表示dja ...
- 9.quartus_warning_altera_reserved_tck
编译的时候没有注意,整个工程都可以在板子上跑起来.但是做Powerplay的时候,出现了这个Critical Warning:. Critical Warning: The following clo ...
- 生命周期(vue的钩子函数)
生命周期图示 创建前,创建后,挂载前,挂载后,更新前,更新后,销毁前,销毁后 beforeCreate:function(){ console.log('1-beforeCreate 组件还未被创建' ...
- POJ 1984 Navigation Nightmare(二维带权并查集)
题目链接:http://poj.org/problem?id=1984 题目大意:有n个点,在平面上位于坐标点上,给出m关系F1 F2 L D ,表示点F1往D方向走L距离到点F2,然后给出一系 ...
- LeetCode解题报告—— Number of Islands & Bitwise AND of Numbers Range
1. Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of island ...
- CentOS 7下OpenLDAP编译安装及配置
一.环境 Server:基于CentOS-7-x86_64-1511 Server IP: 172.18.12.203 二.软件获取 OpenLDAP OpenLDAP官网下载地址:http://ww ...
- SEO页面标题Title的优化
我在一个月前改过页面标题(Title),随后表现是:百度网页快照4天不更新,Google正常.而我仅仅是改了两个词组而已.在建博初期,修改Title的最频繁的时期,下面卢松松就我经历的修改Title过 ...
- HTTP协议——请求与响应
摘要:1.HTTPHTTP:HyperTextTransferProtocol,超文本传输协议的缩写,是本地浏览器和服务器之间进行通信的传送协议.基于TCP/IP协议来传送数据,如HTML文件,图片等 ...