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/mariadb学习记录——limit
在mysql/mariadb 中可以用limit来限制查询的条数.例子如下: 1.limit后加一个参数 limit n: //选中查询所有结果中的前两条记录并返回 mysql> ; +---- ...
- ajaxSubmit请求返回数据成功,但是不执行success回调函数
最近项目涉及到附件上传就头痛,一直在用plupload插件在做...ie9偶尔抽风但还是可以的... 然后有个需求,表格每行都有个上传按钮,页面多上传按钮. 一.开始的时候,用plupload做的,多 ...
- Role Helper
using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; using System.Collections.Ge ...
- CentOS6安装各种大数据软件 第二章:Linux各个软件启动命令
相关文章链接 CentOS6安装各种大数据软件 第一章:各个软件版本介绍 CentOS6安装各种大数据软件 第二章:Linux各个软件启动命令 CentOS6安装各种大数据软件 第三章:Linux基础 ...
- JVM,JMM,虚拟机栈,本地方法栈
JVM 虚拟机栈 本地方法栈:本地方法(使用native关键词修饰的方法,是由JVM底层用C,C++实现的),运行这部份代码使用的栈就是本地方法栈
- centos 安装 telnet
(转)centos7安装telnet服务 场景:在进行Telnet测试时候,发现无法连接,所以还得把这个软件也安装了 1 CentOS7.0 telnet-server 启动的问题 解决方法: 先 ...
- u-boot-1.1.6第1阶段分析之make smdk2410_config指令
uboot源码中的README文档中介绍要使用uboot必须先进行配置后编译,即先执行make xxx_config命令,然后执行make命令,下面以make smdk2410_config指令为例来 ...
- EntityFramework6.1自动生成复数名称数据表的问题
遇到一个很奇怪的问题,两个程序部署在两个不同的机器上,一个是.net 4.6.1另外一个是.net 4.0的运行时,两个项目都引用了EntityFramework6.1.3.程序分别执行后,4.0环境 ...
- 【java笔记】Calendar.getInstance()是什么意思
Calendar类是个抽象类,因此本身不能被实例化,然而在却创建了Calendar 的对象,但并不是抽象类可以创建对象这个对象并不是Calendar 自身实例,而是其子类实例,这是在getInstan ...
- Caliburn.Micro - IResult and Coroutines
IResult and Coroutines 翻译[三台]:网址[http://home.cnblogs.com/u/3Tai/] Previously, I mentioned that there ...