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,想法就是开一个数组存每个拦截系统当前最高能拦截的导弹高度.输入每个导弹高度的时候就开始处理,遍历每一个拦截系统,一旦最 ...
随机推荐
- 【LeetCode】42. Trapping Rain Water
Trapping Rain Water Given n non-negative integers representing an elevation map where the width of e ...
- APP流氓大法之apk 静默安装
老大要我弄个自动更新,要用到静默安装,网上找到了些大拿的代码,我拿去改吧改吧,先贴出来: /** * 软件静默安装 * @param apkAbsolutePath apk文件所在路径 * @retu ...
- Windows Server 2008的远程控制修改端口,谨防非法远程连接
1.首先在Windows Server 2008服务器系统桌面上依次单击“开始”/“运行”命令,在弹出的系统运行对话框中,输入字符串命令“regedit”,单击回车键后,打开对应系统的注册表编辑界面; ...
- RAC安装重新运行root.sh
rac1执行root.sh成功,rac2执行失败. 在重新执行root.sh前,在rac2上把crs等配置信息删除: # /u01/app//grid/crs/install/rootcrs.pl - ...
- 设置Adobe Reader打开PDF文件保持记忆功能
设置Adobe Reader打开PDF文件保持记忆功能 打开菜单“编辑”->“首选项”. 选择种类中的“文档”,在“打开设置”区域勾上“重新打开文档时恢复上次视图设置(R)”,确定之后就可以在下 ...
- AME_AME审批中子元素的概念和用途(概念)
2014-05-30 Created By BaoXinjian AME: Oracle Approvals Management AME的6个元素的概念和主要作用: Attribue -> ...
- [Android实例] 拖动滑块进行图片拼合验证方式的实现
该篇文章从eoeAndroid搬迁过来的,原文地址:[Android实例] 拖动滑块进行图片拼合验证方式的实现 现在网站上有各种各样的验证码验证方式,比如计算大小,输入图片内容等,今天在一家网站上看到 ...
- 解决windows server2012 评估版本过期,系统会自动关机
解决windows server2012 评估版本过期,系统在1小时后会自动关机. 重置方法:以管理员方式运行命令行,执行slmgr.vbs /rearm,可以使授权延长180天,但是仅能延长5次.
- linux怎么查看一个文件夹的大小
linux查看一个文件夹的大小的命令为: -lh 该文件夹的完整路径 例,查询/var文件夹的大小: -lh /var du 递归查询该路径下所有文件的大小(若不加任何参数,则显示文件夹内的所有文件, ...
- js+json用表格实现简单网站左侧导航
闲暇之余,制作一用表格实现的简单的网站导航条,分享给大家.这里的数据基于json格式,学习json的朋友可以参考下. 调用很简单,只要将数据组织成json格式即可:格式如下: window.onlo ...