Visiting a Friend(思维)
Description
Pig is visiting a friend.
Pig's house is located at point 0, and his friend's house is located at point m on an axis.
Pig can use teleports to move along the axis.
To use a teleport, Pig should come to a certain point (where the teleport is located) and choose where to move: for each teleport there is the rightmost point it can move Pig to, this point is known as the limit of the teleport.
Formally, a teleport located at point x with limit y can move Pig from point x to any point within the segment [x; y], including the bounds.

Determine if Pig can visit the friend using teleports only, or he should use his car.
Input
The first line contains two integers n and m (1 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of teleports and the location of the friend's house.
The next n lines contain information about teleports.
The i-th of these lines contains two integers ai and bi (0 ≤ ai ≤ bi ≤ m), where ai is the location of the i-th teleport, and bi is its limit.
It is guaranteed that ai ≥ ai - 1 for every i (2 ≤ i ≤ n).
Output
Print "YES" if there is a path from Pig's house to his friend's house that uses only teleports, and "NO" otherwise.
You can print each letter in arbitrary case (upper or lower).
Sample Input
3 5
0 2
2 4
3 5
YES
3 7
0 4
2 5
6 7
NO
Hint
The first example is shown on the picture below:

Pig can use the first teleport from his house (point 0) to reach point 2, then using the second teleport go from point 2 to point 3, then using the third teleport go from point 3 to point 5, where his friend lives.
The second example is shown on the picture below:

You can see that there is no path from Pig's house to his friend's house that uses only teleports.
题目意思:小猪要去拜访朋友,他的房子在x轴0点,朋友的房子位于m点。小猪要使用传送点去拜访朋友,能到朋友家就输出YES,否则输出NO,输入n,然后输入n行传送点和最远能传送的地点[ai,bi],ai是传送地点,bi是传送到的极限。
解题思路;一看到这道题我一开始想到的是会场安排问题,贪心的策略,但是这道题不是这样的,我一开始是想判断彼此相邻的两端是否有空隙,有空隙的话就不能到达,但是这是不对的,前面的极限有可能会很大覆盖掉后面的传送点!!!其实想到这里了,再想一步就可以解决了,我们只需要不断更新x(最远传送距离),每一次传送,如果传送极限大于x就更新,如果x>=n就能够到达。
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int n,m,flag,i,j,r,l;
flag=;
scanf("%d%d",&m,&n);
for(i=; i<m; i++)
{
scanf("%d%d",&l,&r);
if(flag>=l)
{
flag=max(flag,r);
}
}
if(flag>=n)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
return ;
}
Visiting a Friend(思维)的更多相关文章
- 2017ICPC北京赛区网络赛 Visiting Peking University(简单思维)
描述 Ming is going to travel for n days and the date of these days can be represented by n integers: 0 ...
- [C#][算法] 用菜鸟的思维学习算法 -- 马桶排序、冒泡排序和快速排序
用菜鸟的思维学习算法 -- 马桶排序.冒泡排序和快速排序 [博主]反骨仔 [来源]http://www.cnblogs.com/liqingwen/p/4994261.html 目录 马桶排序(令人 ...
- Photoshop、Illustrator思维导图笔记
半年前学习Photoshop时记得的思维导图笔记,可能不是很全,常用的基本都记下了.
- CYQ.Data 从入门到放弃ORM系列:开篇:自动化框架编程思维
前言: 随着CYQ.Data 开始回归免费使用之后,发现用户的情绪越来越激动,为了保持这持续的激动性,让我有了开源的念头. 同时,由于框架经过这5-6年来的不断演进,以前发的早期教程已经太落后了,包括 ...
- 计算机程序的思维逻辑 (8) - char的真正含义
看似简单的char 通过前两节,我们应该对字符和文本的编码和乱码有了一个清晰的认识,但前两节都是与编程语言无关的,我们还是不知道怎么在程序中处理字符和文本. 本节讨论在Java中进行字符处理的基础 - ...
- 计算机程序的思维逻辑 (29) - 剖析String
上节介绍了单个字符的封装类Character,本节介绍字符串类.字符串操作大概是计算机程序中最常见的操作了,Java中表示字符串的类是String,本节就来详细介绍String. 字符串的基本使用是比 ...
- 计算机程序的思维逻辑 (31) - 剖析Arrays
数组是存储多个同类型元素的基本数据结构,数组中的元素在内存连续存放,可以通过数组下标直接定位任意元素,相比我们在后续章节介绍的其他容器,效率非常高. 数组操作是计算机程序中的常见基本操作,Java中有 ...
- 计算机程序的思维逻辑 (33) - Joda-Time
Joda-Time上节介绍了JDK API中的日期和时间类,我们提到了JDK API的一些不足,并提到,实践中有一个广泛使用的日期和时间类库,Joda-Time,本节我们就来介绍Joda-Time.俗 ...
- 计算机程序的思维逻辑 (53) - 剖析Collections - 算法
之前几节介绍了各种具体容器类和抽象容器类,上节我们提到,Java中有一个类Collections,提供了很多针对容器接口的通用功能,这些功能都是以静态方法的方式提供的. 都有哪些功能呢?大概可以分为两 ...
随机推荐
- MySQL----MySQL数据库入门----第四章 单表查询
select [distinct] * | 字段1,字段2,字段3... from 表名 [where 条件表达式] [group by 字段名] [having 条件表示式] [order by 字 ...
- echarts动态加载数据无法更新series 无法更新图表
最近遇到一个Echarts图表无法动态更新数据的问题 最初我在option中设置series的值为一个数组,想着通过修改数组来动态更新图表,但是没变 化,后来发觉是因为图表数据会和之前的合并 看官方的 ...
- mongodb的docker化安装
查询mongo镜像 docker search mongo 拉取镜像(拉取STARS最多的那个就可以了) docker pull mongo tips:如果拉取不成功,多pull几次就可以了. 使用自 ...
- HAProxy负载均衡策略
HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性.负载均衡,以及基于TCP和HTTP的应用程序代理.HAProxy是支持虚拟主机的,HAProxy的优点能够补充Nginx的一些 ...
- Redis底层数据类型
Redis主要数据结构:简单动态字符串(SDS).双端链表.字典.跳跃表.整数集合.压缩列表和快速列表: 一.简单动态字符串(SDS): Redis没有直接使用C语言中的传统的字节数组保存字符串,而是 ...
- dtree的自定义select动作
项目中用到了dtree,别问我为什么用这么古老的插件,因为简单啊orz,文件树的条目不多,detree加载卡顿的问题也不用解决,开森. 在使用过程中在选择节点后需要自定义一些onclick的动作,本来 ...
- BrightScript 3D test - Roku (4)
My initial attempt to port over an old Actionscript program, here it goes in main.brs. Library " ...
- Could not connect to '192.168.80.145' (port 22): Connection failed的解决办法(远程连不上xshell)
问题状况表现1 这个问题一般是你 的什么配置影响了虚拟机的网卡网关设置!!!. 问题状况表现2 这个问题一般是你 的什么配置影响了虚拟机的网卡网关设置. 解决办法 网上的那些解决方案,我都试过,比如. ...
- 20155227 2016-2017-2 《Java程序设计》第三周学习总结
20155227 2016-2017-2 <Java程序设计>第三周学习总结 教材学习内容总结 类与对象 使用Java撰写程序几乎都在使用对象,要产生对象必须先定义类,类是对象的设计图,对 ...
- 20155232 2016-2017-3 《Java程序设计》第4周学习总结
20155232 2016-2017-3 <Java程序设计>第4周学习总结 教材学习内容总结 第六章 继承与多态 所谓继承就是避免多个类间重复定义共同行为. 1.重复在程序设计上就是不好 ...