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 ...
随机推荐
- 【LA】5135 Mining Your Own Business
[算法]点双连通分量 [题解]详见<算法竞赛入门竞赛入门经典训练指南>P318-319 细节在代码中用important标注. #include<cstdio> #includ ...
- ios的app,有新版本时必须先更新。
现在没时间整理,先把代码贴出来,以后再做详细的思路整理. 1 在AppController.mm的didFinishLaunchingWithOptions方法里面获取本地应用版本信息,保存起来. / ...
- 安装magento配置完数据库后出现: PHP Extensions "0" must be loaded. 解决方案
打开magento目录下的该文件: app/code/core/Mage/Install/etc/config.xml 将下面该段修改: <mysql4> <type>pdo_ ...
- winform Textbox像百度一下实现下拉显示
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 关于win7局域网共享的相关设置
模式1> 被访问方相关设置步骤: (1)被共享方的电脑开通来宾用户 (2)被共享方的电脑的本地安全策略需要设置成 "仅来宾" (3)被共享方的电脑高级共享设置中 " ...
- JavaScript match() 方法
match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配. 该方法类似 indexOf() 和 lastIndexOf(),但是它返回指定的值,而不是字符串的位置. var st ...
- 一个无线通信类投稿的期刊list
转载一个,但是有些期刊的影响因子不是很对,要投的时候还是再到期刊主页上面看一看吧~ 期刊缩写 期刊全名 近年影响因子 P IEEE Proceedings Of The IEEE 3.686 IEEE ...
- Django 国内最全教程
https://code.ziqiangxuetang.com/django/django-tutorial.html
- HDU 5136 Yue Fei's Battle
题目链接:HDU-5136 网上的一篇题解非常好,所以就直接转载了.转自oilover的博客 代码: #include<cstring> #include<cstdio> #i ...
- ASP.NET 163 smtp服务器响应为:User has no permission
1.问题引出 今天在asp.net程序中,利用System.Net.Mail.MailMessage类和网易163免费邮箱服务器发送邮件时出现了如下问题. 2.解决方案 原因很简单,我们在asp.ne ...