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,想法就是开一个数组存每个拦截系统当前最高能拦截的导弹高度.输入每个导弹高度的时候就开始处理,遍历每一个拦截系统,一旦最 ...
随机推荐
- php计算经纬度间的距离
<?php //根据经纬度计算距离 function getdistance($lng1,$lat1,$lng2,$lat2) { //将角度转为狐度 $radLat1=deg2rad($lat ...
- jquery ajax调用WCF,采用System.ServiceModel.WebHttpBinding
Jquery ajax调用WCF服务 例子效果如下:原界面 点击按钮GetList get后,通过指定的Url获取数据添加到table 新建一个控制台项目,添加IContract.cs,DBServi ...
- 计算机科学基础知识(一)The Memory Hierarchy
一.前言 最近一个问题经常萦绕在我的脑海:一个学习电子工程的机械师如何称为优秀的程序员?(注:本文作者本科学习机械设计,研究生转到电子工程系学习,毕业后却选择了系统程序员这样的职业).经过思考,我认为 ...
- Linux时间子系统(三) 用户空间接口函数
一.前言 从应用程序的角度看,内核需要提供的和时间相关的服务有三种: 1.和系统时间相关的服务.例如,在向数据库写入一条记录的时候,需要记录操作时间(何年何月何日何时). 2.让进程睡眠一段时间 3. ...
- winform dataGridView DataGridViewComboBoxColumn 下拉框事件
有一个dataGridView ,有一列是DataGridViewComboBoxColumn .用动态绑定,在绑定数据的时候.我们也给这一列绑定数据 在dataGridView的RowsAdded事 ...
- MVC 之Action
下面我要重新温习一下,MVC控制器中的Action方法相关概念: 1.Action方法必须是public,不能是private或者是protected; 2.Action方法不能够被重载: 3.Act ...
- 连接mysql-front数据库出现‘执行错误1251’的解决办法(有效)
原因是MySQL8.0版本的加密方式和MySQL5.0的不一样,连接会报错. 解决方法:更改加密方式 控制台中先连接mysql: 再输入: ALTER USER 'root'@'localhost' ...
- Latex中定义、定理、引理、证明 设置方法总结
Latex中定义.定理.引理.证明 设置方法总结 在LaTex中需要有关定理.公理.命题.引理.定义等时,常用如下命令 \newtheorem{定理环境名}{标题}[主计数器名] \newtheore ...
- redis 基本概览
1 redis 支持5种数据类型.redis 的值是字节数组,所以他不关心具体存储的是什么. redis 的键可以是 数值 strings lists sets hashes Redis 支持诸如列表 ...
- 【JEECG_3.7.1】列表多表头的设计
先看下多表头的设计: 在这个多表头的表单当中,我们可以按照从上到下和从左往右的划分方式,将表头划分成三行十列,分别是: 列表标签 人员信息.部门信息.工资.入职状态.创建日期.操作 名称.年龄.性别. ...