CodeForces - 556D Case of Fugitive (贪心+排序)
Andrewid the Android is a galaxy-famous detective. He is now chasing a criminal hiding on the planet Oxa-5, the planet almost fully covered with water.
The only dry land there is an archipelago of n narrow islands located in a row. For more comfort let's represent them as non-intersecting segments on a straight line: island i has coordinates [li, ri], besides, ri < li + 1 for 1 ≤ i ≤ n - 1.
To reach the goal, Andrewid needs to place a bridge between each pair of adjacentislands. A bridge of length a can be placed between the i-th and the (i + 1)-th islads, if there are such coordinates of x and y, that li ≤ x ≤ ri, li + 1 ≤ y ≤ ri + 1and y - x = a.
The detective was supplied with m bridges, each bridge can be used at most once. Help him determine whether the bridges he got are enough to connect each pair of adjacent islands.
Input
The first line contains integers n (2 ≤ n ≤ 2·105) and m (1 ≤ m ≤ 2·105) — the number of islands and bridges.
Next n lines each contain two integers li and ri (1 ≤ li ≤ ri ≤ 1018) — the coordinates of the island endpoints.
The last line contains m integer numbers a1, a2, ..., am (1 ≤ ai ≤ 1018) — the lengths of the bridges that Andrewid got.
Output
If it is impossible to place a bridge between each pair of adjacent islands in the required manner, print on a single line "No" (without the quotes), otherwise print in the first line "Yes" (without the quotes), and in the second line print n - 1numbers b1, b2, ..., bn - 1, which mean that between islands i and i + 1 there must be used a bridge number bi.
If there are multiple correct answers, print any of them. Note that in this problem it is necessary to print "Yes" and "No" in correct case.
Examples
4 4
1 4
7 8
9 10
12 14
4 5 3 8
Yes
2 3 1
2 2
11 14
17 18
2 9
No
2 1
1 1
1000000000000000000 1000000000000000000
999999999999999999
Yes
1
Note
In the first sample test you can, for example, place the second bridge between points 3 and 8, place the third bridge between points 7 and 10 and place the first bridge between points 10 and 14.
In the second sample test the first bridge is too short and the second bridge is too long, so the solution doesn't exist.
题目大意:
给你n-1个区间和m个点。问你能否从m个点中找出n-1个点,分别包含于n-1个区间中。如果能,输出Yes,并输出任何一种匹配结果;否则输出No。
贪心。
先将区间按其左端点排序,再将点按其坐标排序。
依次考察每个点,直至考察完所有点(1..m):
1、将所有左端点不大于该点的区间送入优先队列。区间右端点越小,优先级越高。
2、若队列非空,则pop。若区间右端点比该点小,则跳出循环(实际上已经可以输出No了,因为后面没有更小的点与该区间对应);若区间右端点不小于该点,则建立区间到这点的对应关系(贪心,因为右端点较大的区间更有可能匹配到较大的点)。
最后考察是否所有的区间都建立了到点的对应关系,是则输出Yes及对应关系;否则输出No。
排序(谈几点自己的理解,C++学完对运算符重载啥的了解后,再回来想一想)。
//结构体&数
//优先队列用结构体:struct cmp & greater/less<int>
//排序用函数:int cmp() & int cmp()
//如果只涉及一种偏序关系,不必太在意,也可以在结构体内部写,如果涉及偏序关系较多,最好拿出来写清楚
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm> typedef long long lol; using namespace std; const int maxn=; struct tnode
{
lol mmin;
lol mmax;
int seq;
};
tnode node[maxn+];//1..n-1的区间 struct tlen
{
lol lenth;
int seq;
};
tlen len[maxn+];//1..m的桥长 int cmp1(tnode a,tnode b)
{
return a.mmin<b.mmin;
} int cmp2(tlen a,tlen b)
{
return a.lenth<b.lenth;
} struct cmp3
{
bool operator()(const int a,const int b)
{
return node[a].mmax>node[b].mmax;
}
}; int bridge[maxn+];//i区间对应的桥 int main()
{
int n,m;
scanf("%d%d",&n,&m); lol l0,r0,l,r;
for(int i=;i<=n;i++)
{
scanf("%lld%lld",&l,&r);
if(i>=)
{
node[i-].mmin=l-r0;
node[i-].mmax=r-l0;
node[i-].seq=i-;
}
l0=l;
r0=r;
}
for(int i=;i<=m;i++)
{
scanf("%lld",len+i);
len[i].seq=i;
} sort(node+,node+n,cmp1);
sort(len+,len+m+,cmp2); priority_queue<int,vector<int>,cmp3> q;
memset(bridge,,sizeof(bridge));
for(int i=,j=;i<=m;i++)
{
for(;j<=n-&&node[j].mmin<=len[i].lenth;j++)
q.push(j);
bool flag=true;
if(!q.empty())
{ int tmp=q.top();q.pop();
if(node[tmp].mmax<len[i].lenth)
flag=false;
else
bridge[node[tmp].seq]=len[i].seq;
}
if(!flag)
break;
} bool flag=true;
for(int i=;i<=n-;i++)
if(bridge[i]==)
{
flag=false;
break;
}
if(flag)
{
printf("Yes\n");
for(int i=;i<=n-;i++)
{
if(i<n-)
printf("%d ",bridge[i]);
else
printf("%d\n",bridge[i]);
}
}
else
printf("No\n"); return ;
}
CodeForces - 556D Case of Fugitive (贪心+排序)的更多相关文章
- 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 555b//Case of Fugitive// Codeforces Round #310(Div. 1)
题意:有n-1个缝隙,在上面搭桥,每个缝隙有个ll,rr值,ll<=长度<=rr的才能搭上去.求一种搭桥组合. 经典问题,应列入acm必背300题中.属于那种不可能自己想得出来的题.将二元 ...
- 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 555 B. Case of Fugitive
\(>Codeforces \space 555 B. Case of Fugitive<\) 题目大意 : 有 \(n\) 个岛屿有序排列在一条线上,第 \(i\) 个岛屿的左端点为 \ ...
- 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
D. Case of Fugitive time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- codeforces Gym 100338E Numbers (贪心,实现)
题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...
- [Codeforces 1214A]Optimal Currency Exchange(贪心)
[Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...
随机推荐
- centos 更换用户密码
腾讯云报告了我的服务器被暴力破解了.... 因此需要更换更复杂的password, 命令为:passwd 用户名,例如下我要更换root的password [root@VM_0_4_centos ~ ...
- 【Linux系列】Centos 7部署Laravel项目(七)
目的 本文主要介绍以下五点: 一. Composer安装 二. SSH设置 三. Git安装 四. Laravel部署 五. 上传GitHub 演示 一. Composer安装 # cd /usr/l ...
- python3 之 迭代器与生成器
迭代器 迭代是Python最强大的功能之一,是访问集合元素的一种方式. 迭代器是一个可以记住遍历的位置的对象. 迭代器对象从集合的第一个元素开始访问,知道所有的元素被访问完结束. 迭代器只能往前不会后 ...
- 【NHOI2018】找素数
[题目描述] 素数又称质数,是指一个大于 1 的正整数,如果除了 1 和它本身以外,不能再被其它的数整除,例如:2.3.5.97 等都是素数.2 是最小的素数. 现在,给你 n 个数字,请你从中选取一 ...
- 使用 buildx 构建多平台 Docker 镜像
原文链接:使用 buildx 构建多平台 Docker 镜像 在工作和生活中,我们可能经常需要将某个程序跑在不同的 CPU 架构上,比如让某些不可描述的软件运行在树莓派或嵌入式路由器设备上.特别是 D ...
- 邮箱基础协议:SMTP/POP3/IMAP
目录 电子邮件的组成:信封.首部和正文 邮件基础协议 SMTP SMTP 指令 使用 Telnet 模拟 SMTP 发送邮件 POP3 POP3 的生命周期 IMAP 标志消息属性 状态和流程图 IM ...
- alloc 和 init都做了什么验证。
结论: alloc负责分配内存和创建对象对应的isa指针: init只是返回alloc生成的对象. 所以alloc后,多次调用init,返回的对象是同一个! 代码如下: // // main.m / ...
- 不使用cookie记录用户信息
cookie是什么: cookie是由web服务器保存在用户浏览器(客户端)上的小文件,它可以包含用户信息,用户操作信息等等,无论何时访问服务器,只要同源,就能携带到服务端 常见方式 一般:请求一个接 ...
- OpenCV图像识别初探-50行代码教机器玩2D游戏【华为云技术分享】
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/devcloud/article/detai ...
- 机器学习算法在用户行为检测(UBA)领域的应用
[摘要]最近看到越来越多的安全圈的同学开始关注UBA或者UEBA的相关产品和技术,恰好这一段时也一直在跟进UBA产品的状况,正如Gartner报告所述,最具创新能力的UBA供应商往往都是一些初创公司, ...