14B - Young Photographer

B. Young Photographer
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

Among other things, Bob is keen on photography. Especially he likes to take pictures of sportsmen. That was the reason why he placed himself in position x0 of a long straight racetrack and got ready to take pictures. But the problem was that not all the runners passed him. The total amount of sportsmen, training at that racetrack, equals n. And each of them regularly runs distances within a particular segment of the racetrack, which is the same for each sportsman. For example, the first sportsman runs from position a1 to position b1, the second — from a2 to b2

What is the minimum distance that Bob should move to have a chance to take pictures of each sportsman? Bob can take a picture of a sportsman, if he stands within the segment that this sportsman covers on the racetrack.

Input

The first line of the input file contains integers n and x0 (1 ≤ n ≤ 1000 ≤ x0 ≤ 1000). The following n lines contain pairs of integersai, bi (0 ≤ ai, bi ≤ 1000ai ≠ bi).

Output

Output the required minimum distance in the same units as the positions on the racetrack. If there is no such a position, output -1.

Examples
input
3 3
0 7
14 2
4 6
output
1


给出一些区间,和一个点,问这些区间的公共部分距离这个点的最近距离,如果不存在,那么输出-1


各种小细节错误,各种不注意


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
    int n,m,x[1005]={0};
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;++i)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        if(a>b)
        {
            swap(a,b);
        }
        for(int j=a;j<=b;++j)
        {
            ++x[j];
        }
    }
    int bg=0,ed=0,i=0;
    while(i<1005&&x[i]!=n)
    {
        ++i;
    }
    bg=i;
    while(i<1005&&x[i]==n)
    {
        ++i;
    }
    ed=i-1;
    int ans;
    if(bg==1005)
    {
        ans=-1;
    }
    else
    {
        if(m>=bg&&m<=ed)
        {
            ans=0;
        }
        else
        {
            ans=min(abs(m-bg),abs(m-ed));
        }
    }
    printf("%d\n",ans);
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值