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 ...
随机推荐
- 【Foreign】咏叹 [模拟退火]
咏叹 Time Limit: 100 Sec Memory Limit: 256 MB Description 有n根木棍,第i根长度为ai.你要贴着墙围出一个矩形区域,木棍围成的矩形边缘必须平行或 ...
- Connections between cities(LCA)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2874 题目: Problem Description After World War X, a lot ...
- HDU 2084 数塔 (dp)
题目链接 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数 ...
- for in、each; for 、forEach、map
1.jQuery.each(object, [callback]) 用于例遍任何对象.回调函数拥有两个参数:第一个为对象的成员或数组的索引,第二个为对应变量或内容.如果需要退出 each 循环可使回调 ...
- 原生ES-Module在浏览器中的尝试
其实浏览器原生模块相关的支持也已经出了一两年了(我第一次知道这个事情实在2016年下半年的时候) 可以抛开webpack直接使用import之类的语法 但因为算是一个比较新的东西,所以现在基本只能自己 ...
- Spring Cloud Netflix之Eureka 相关概念
为什么不应该使用ZooKeeper做服务发现 英文链接:Eureka! Why You Shouldn’t Use ZooKeeper for Service Discovery:http://www ...
- selenium.webdriver.common.keys 模块中常用的变量
表11-5 selenium.webdriver.common.keys 模块中常用的变量属性 含义Keys.DOWN, Keys.UP, Keys.LEFT,Keys.RIGHT 键盘箭头键Keys ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 动态树
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2002 题意:加边,删边,查询到根的距离. #include <bits/stdc++ ...
- 如何让Footer无论页面长短都在最底部, 并和正文保持固定高度?
html结构: <div id="container"> <div id="content">页面正文</div> < ...
- 数据库SQL实战(1)
1.查找最晚入职员工的所有信息: CREATE TABLE `employees` ( `emp_no` int(11) NOT NULL, `birth_date` date NOT NULL, ` ...