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 ...
随机推荐
- 【CF1009F】 Dominant Indices (长链剖分+DP)
题目链接 \(O(n^2)\)的\(DP\)很容易想,\(f[u][i]\)表示在\(u\)的子树中距离\(u\)为\(i\)的点的个数,则\(f[u][i]=\sum f[v][i-1]\) 长链剖 ...
- php中比较好用的函数
PHP中一个好用的函数parse_url,特别方便用来做信息抓取的分析,举例子如下: $url = "http://www.electrictoolbox.com/php-extract-d ...
- css纯样式导航
<style>.dropdown { position: relative; display: inline-block;} .dropdown-content { di ...
- ajax中datatype的json和jsonp
前言: 说到AJAX就会不可避免的面临两个问题,第一个是AJAX以何种格式来交换数据?第二个是跨域的需求如何解决?这两个问题目前都有不同的解决方案,比如数据可以用自定义字符串或者用XML来描述,跨域 ...
- Gulp、Grunt构建工具
在Gulp中创建一个库从磁盘gulp.src读取源文件并通过磁盘管道写回内容到gulp.dest,可以理解成只是将文件复制到另一个目录. var gulp = require('gulp'); gul ...
- js_判断当前url是否合法http(s)
alert(checkURL('http:555')); //false function checkURL(URL) { var str = URL, Expression = /http(s)?: ...
- 5.0docer 网络链接
docker0 :linux的虚拟网桥 虚拟网桥特点: 1.可以设置ip地址 2.相当于拥一个隐藏的虚拟网卡 安装网桥工具 apt-get install bridge-utils brctl ...
- 破解邻居家的wifi密码
刚刚学习了如何破解wifi密码 然后昨天晚上连续破解了两个 好激动 我是在ubuntu上面使用aircrack-ng套件进行破解的 首先进行抓包,然后跑字典就ok了 下面的不错 一.关闭网络和结束可能 ...
- Spark-2.3.2【SparkStreaming+SparkSQL-实时仪表盘应用】
应用场景:实时仪表盘(即大屏),每个集团下有多个mall,每个mall下包含多家shop,需实时计算集团下各mall及其shop的实时销售分析(区域.业态.店铺TOP.总销售额等指标)并提供可视化展现 ...
- nmap导出处理脚本
import sys log = open("result.gnmap","r") xls = open("output.csv",&quo ...