CF555B 贪心
http://codeforces.com/problemset/problem/555/B
3 seconds
256 megabytes
standard input
standard output
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 adjacent islands. 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 + 1 and 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.
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.
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 - 1 numbers 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.
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
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.
/**
CF555B 贪心
题目大意:给定一些不连续的区间用给定长度的线段将这些区间连起来。要求线段长度的两个顶点必须在连接的两个区间上,
问能否够将全部的区间连接成一个区间
解题思路:对于每个要连接的空隙(即相邻的两个区间之间的部分)求出可用线段的最短长度x和最长长度y,依照x递减,x相等y递减的
方式排序。将须要的线段放到给set中。从前到后遍历空隙。每次取第一个长度小于等于y的线段。假设改线段大于等于x。
则循环继续进行,否则跳出循环,输出No
*/
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <set>
using namespace std;
typedef long long LL;
pair <pair<LL,LL>,int>p[200005];
set<pair<LL,int> >mp;
set<pair<LL,int> >::iterator k;
int s[200005],n,m; int main()
{
LL u,v,x,y;
while(~scanf("%d%d",&n,&m))
{
for(int i=0; i<n; i++)
{
scanf("%I64d%I64d",&x,&y);
if(i>0)
{
p[i]=make_pair(make_pair(v-x,u-y),i);
///printf("%d->%I64d %I64d\n",i,p[i].first.first,p[i].first.second);
}
u=x;
v=y;
}
mp.clear();
for(int i=1; i<=m; i++)
{
scanf("%I64d",&x);
mp.insert(make_pair(x,i));
}
sort(p+1,p+n);
int flag=1;
for(int i=1; i<n; i++)
{
x=-p[i].first.first;
y=-p[i].first.second;
//printf("%I64d %I64d\n",x,y);
k=mp.lower_bound(make_pair(y,1<<30));
if(k==mp.begin())
{
puts("No");
flag=0;
break;
}
k--;
if(k->first<x)
{
puts("No");
flag=0;
break;
}
s[p[i].second]=k->second;
mp.erase(k);
}
if(flag)
{
puts("Yes");
for(int i=1; i<n; i++)
{
printf(i==n-1?"%d\n":"%d ",s[i]);
}
}
}
return 0;
}
CF555B 贪心的更多相关文章
- BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]
1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1383 Solved: 582[Submit][St ...
- HDOJ 1051. Wooden Sticks 贪心 结构体排序
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]
1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 786 Solved: 391[Submit][S ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【BZOJ-4245】OR-XOR 按位贪心
4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 486 Solved: 266[Submit][Sta ...
- code vs 1098 均分纸牌(贪心)
1098 均分纸牌 2002年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 有 N 堆纸牌 ...
- 【BZOJ1623】 [Usaco2008 Open]Cow Cars 奶牛飞车 贪心
SB贪心,一开始还想着用二分,看了眼黄学长的blog,发现自己SB了... 最小道路=已选取的奶牛/道路总数. #include <iostream> #include <cstdi ...
- 【贪心】HDU 1257
HDU 1257 最少拦截系统 题意:中文题不解释. 思路:网上有说贪心有说DP,想法就是开一个数组存每个拦截系统当前最高能拦截的导弹高度.输入每个导弹高度的时候就开始处理,遍历每一个拦截系统,一旦最 ...
随机推荐
- vi 删除全部内容
非插入模式下删除所有内容 a.光标移到第一行,然后按10000后然后点dd b.光标移到第一行,按下dG 命令输入模式下删除所有内容 a.输入命令.,$d,回车 b.输入命令1,999dd,回车
- Hadoop分布式文件系统:架构和设计
原文地址:http://hadoop.apache.org/docs/r1.0.4/cn/hdfs_design.html 引言 前提和设计目标 硬件错误 流式数据访问 大规模数据集 简单的一致性模型 ...
- Eclipse安装goclipse插件方法
第一步:打开菜单栏“Help”-----"Eclipse Maketplace". 第二部:在弹出框的Find框中输入GoClipse,等待搜索结果出来后结果如下: 第三步:点击“ ...
- 什么是FSK制式?什么是DTMF制式?
目前国内来电显示制式有FSK.DTMF(双音频)两种,普通推广的是FSK."来电显示"又称"主叫号码显示"(Calling Identity Delivery) ...
- android适配器Adapter
一.什么是适配器,适配器有什么用? 适配器是AdapterView视图(如ListView - 列表视图控件.Gallery - 缩略图浏览器控件.GridView - 网格控件.Spinner - ...
- Linux进程概述
一.介绍 当linux系统中的一个进程运行起来的时候,总是要访问系统的资源,访问文件或者向其他的进程发送信号.系统是否允许其进行这些操作?系统是根据什么来判断该进程的权限?这些问题是和进程信任状(pr ...
- 基于Linux的USB 主/从设备之间通讯的三种方式
转载:http://archive.eet-china.com/www.eet-china.com/ART_8800323770_617693_TA_eda530e7.HTM 随着简单易用的USB接口 ...
- PHP函数register_shutdown_function的使用示例
某些情况下,我们需要在程序执行结束时,做一些后续的处理工作,这个时候,php的register_shutdown_function函数就可以帮我们来实现这个功能. 函数简介 当PHP程序执行完成后,自 ...
- 如何让div在整个页面中居中?
参考必应主页的样式,在页面中放置一个表格,100%宽高,然后表格内部是一个单元格,在单元格内部放置div,使其margin为auto即可. <table style="height: ...
- [转]WCF类型共享技巧
调用过WCF服务的同学可能都会遇到这样的问题,同一个实体类型,不同的服务Visual Studio生成了不同的版本,例如Service1.User和Service2.User,对于C#来说,这是两个不 ...