Codeforces Beta Round #14 (Div. 2) B. Young Photographer 水题

B. Young Photographer

题目连接:

http://codeforces.com/contest/14/problem/B

Description

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 ≤ 100; 0 ≤ x0 ≤ 1000). The following n lines contain pairs of integers ai, bi (0 ≤ ai, bi ≤ 1000; ai ≠ 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.

Sample Input

3 3
0 7
14 2
4 6

Sample Output

1

Hint

题意

有n个区间,然后现在你在x,你需要找到一个最近的位置,这个位置覆盖了所有的区间。

题解:

线段树什么的都可以啦

对于每个区间,我都打一个标记就好了,如果现在的标记数量有n个,就说明覆盖了n次

然后扫一遍就好了

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1006;
int a[maxn];
int main()
{
    int n,x;
    scanf("%d%d",&n,&x);
    for(int i=1;i<=n;i++)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        if(l>r)swap(l,r);
        a[l]++;
        a[r+1]--;
    }
    int tmp = 0;
    int ans = 1e9;
    for(int i=0;i<=1000;i++)
    {
        tmp+=a[i];
        if(tmp==n)ans=min(ans,abs(i-x));
    }
    if(ans==1e9)cout<<"-1"<<endl;
    else cout<<ans<<endl;
}
    原文作者:qscqesze
    原文地址: https://www.cnblogs.com/qscqesze/p/5844985.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞