14B - Young Photographer

来源:互联网 发布:淘宝怎么做百度推广 编辑:程序博客网 时间:2024/04/29 15:32

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 30 714 24 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;}




0 0
原创粉丝点击