题源:https://cn.vjudge.net/problem/1268616/origin
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
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).≥ ai - 1 for every i (2 ≤ i ≤ n).
Input
3 5
0 2
2 4
3 5
Output
YES
Input
3 7
0 4
2 5
6 7
Output
NO
——————————————————————————————————
思路:一个坐标轴,pig同学从家(0)出发,到朋友家,但是他走的不是寻常路,走的是传送门,只要第n个的传送左界在之前任意一个传送门右界的左侧或相等,就可以传到n的右界,直到任意一个传送门的右界大于等于朋友家坐标即可。
所以这里我们知道是不断更新能传送到的最大值,而不是不管最新的值是不是最大值就更新。所以我们采取更新最大值(最远)的方法,而不是模拟传送过程的方法。
AC代码如下:
#include<stdio.h>
int main()
{
int n,m,l[101],r[101],i,flag=1,max=0;
scanf("%d%d",&n,&m);
l[0]=r[0]=0; //这里假定第0个的右界是0 主要是防止输入的第一个左界大于1
for(i=1;i<n+1;i++) //输入n个坐标
scanf("%d%d",&l[i],&r[i]);
if(r[0]<l[1]) {printf("NO");return 0;} //当然这里l[1]也可以直接和0比较就不设l[0]和r[0]了
else
for(i=1;i<n;i++)
{
if(r[i]>=m) break;
if(max<r[i]) max=r[i];
if(max<l[i+1]) {flag=0;break;} // 断路 接不上下一个了
}
if(i==n&&r[n]<m) flag=0; //循环完所有的才退出 所以这里r[n]就是max,最大值都到不了朋友家 所以flag置0
if(flag) printf("YES"); //是可以到的!!
else printf("NO");
return 0;
}