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,想法就是开一个数组存每个拦截系统当前最高能拦截的导弹高度.输入每个导弹高度的时候就开始处理,遍历每一个拦截系统,一旦最 ...
随机推荐
- SpringBoot自定义HttpMessageConverter
Spring就是一个大大的插线板,上面插着各种各样的Bean. SpringBoot大大简化了Spring的配置,将原来放在XML中的配置大量的在代码中使用注解实现.这么做有利有弊,总体上利大于弊. ...
- Error parsing XML: not well-formed (invalid token) 报错
鼠标右键选择Source然后再选Format 就可以解决此问题
- Linux内核配置解析 - 概述(基于ARM64架构)
1. 前言 对刚接触Linux kernel的同学来说,遇到的第一个问题就是:我该从哪里入手?. 话说Linux kernel的打开方式是多种多样的:从简单的设备驱动入手:从源代码的目录结构入手:从k ...
- Linux内核中锁机制之RCU、大内核锁
在上篇博文中笔者分析了关于完成量和互斥量的使用以及一些经典的问题,下面笔者将在本篇博文中重点分析有关RCU机制的相关内容以及介绍目前已被淘汰出内核的大内核锁(BKL).文章的最后对<大话Linu ...
- Linux内核中锁机制之内存屏障、读写自旋锁及顺序锁
在上一篇博文中笔者讨论了关于原子操作和自旋锁的相关内容,本篇博文将继续锁机制的讨论,包括内存屏障.读写自旋锁以及顺序锁的相关内容.下面首先讨论内存屏障的相关内容. 三.内存屏障 不知读者是是否记得在笔 ...
- WIN7下恼人的AppData——删除没用的缓存文件
今日.打开电脑,发现C盘可用容量居然变得非常小.认为非常是可疑,例如以下图所看到的: 最初的反应是电脑中毒了,于是使用360卫士.360杀毒对C盘查杀,由于明明记得C盘有40多G的可用空间才对.出现这 ...
- telnet远程登录协议
什么是Telnet? 对于Telnet的认识,不同的人持有不同的观点,可以把Telnet当成一种通信协议,但是对于入侵者而言,Telnet只是一种远程登录的工具.一旦入侵者与远程主机建立了Telnet ...
- RabbitMQ 远程 IP 访问 解决办法 -摘自网络
刚刚安装的RabbitMQ-Server-3.3.5,并且也已经开启了Web管理功能,但是现在存在一个问题: 出于安全的考虑,guest这个默认的用户只能通过http://localhost:1567 ...
- JDK1.5新特性,基础类库篇,格式化类(Formatter)用法
Formatter类提供了对布局对齐和排列的支持,以及对数值.字符串和日期.时间数据的常规格式和特定于语言环境的输出的支持.通用Java类型,诸如byte.BigDecimal和Calendar都被支 ...
- Js控制弹窗实现在任意分辨率下居中显示
弹窗居中比较烦人的是怎么才能在任意分辨率下实现居中显示.1,html部分 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transition ...