\(>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的更多相关文章

  1. Codeforces 555 C. Case of Chocolate

    \(>Codeforces \space 555 C. Case of Chocolate<\) 题目大意 : 有一块 \(n \times n\) 的倒三角的巧克力,有一个人要吃 \(q ...

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

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

  4. Codeforces 556D - Case of Fugitive

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

  5. codeforces 555B Case of Fugitive

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

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

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

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

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

  8. 【35.37%】【codeforces 556C】Case of Matryoshkas

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. 【66.47%】【codeforces 556B】Case of Fake Numbers

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

随机推荐

  1. Python的异常处理机制 -- (转)

    当你的程序中出现异常情况时就需要异常处理.比如当你打开一个不存在的文件时.当你的程序中有一些无效的语句时,Python会提示你有错误存在. 下面是一个拼写错误的例子,print写成了Print.Pyt ...

  2. linux c 执行新程序

    学习进程时,linu c上说新开的进程一般要执行另外一个程序,同时与父进程执行同一个程序没有意义 如下是如何执行一个新的程序 使用exec函数簇 exec函数簇包含如下函数

  3. metasploit后门维持技术

    在meterpreter中执行:run metsvc -A 如此以后便会自动在服务器当中多生成一个meterpreter的服务,并且是开机自动启动.所以二次如果要利用直接: use exploit/m ...

  4. 任务调度框架kunka

    kunka kunka是一个任务调度框架.用户只需要在Task接口中实现自己要执行的功能,并且选择合适的执行器,放入TaskManager中,就可以了完成整个任务了. 实现细节 整个任务信息存放在内存 ...

  5. DirectX介绍(转)

    原文转自 https://baike.baidu.com/item/Direct3D/910353

  6. 【DLL】动态库的创建,隐式加载和显式加载(转)

    原文转自:https://blog.csdn.net/dcrmg/article/details/53437913

  7. 【Python问题解决】关于解决python3.x无法使用PIL库的解决方法

    因为PIL库目前只更新到python2.x,故python3.x直接安装PIL库会找不到版本.但是python3.x有一个新的库,可以提供和PIL差不多的功能,也就是pillow库. 本人使用的是py ...

  8. ThinkPHP5 模型 - 事务支持

    使用事务之前,先确保数据库的存储引擎支持事务操作. MyISAM:不支持事务,主要用于读数据提高性能 InnoDB:支持事务.行级锁和并发 Berkeley DB:支持事务 ThinkPHP5 使用事 ...

  9. ERROR: do not initialise statics to false

    Question about git commit rule I git commit a patch, The patch has a "static int xxxxxxxxxxxxxx ...

  10. CentOS在ssh下远程重装系统

    CentOS在ssh下远程重装系统 http://www.zxsdw.com/index.php/archives/913/ 国外VPS服务器一般都有控制面板,有很多种系统可自行安装,但国内有些IDC ...