codefroces14B

题目链接http://codeforces.com/problemset/problem/14/B

Young Photographer

Problem 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

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).

Sample Input

3 3
0 7
14 2
4 6

Sample Output

1

题意

Bob站在位置x0处,他想要拍到所有运动员,然后给出所有运动员运动的区间,问Bob至少要移动多少距离,如果怎么样都无法拍到所有人,那就输出-1。

方法

区间范围在1000内,用暴力就行,用个数组存放,在范围内就+1。

代码

#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<cstdlib>
#include<iomanip>
#include<string>
#include<vector>
#include<map>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
int main()
{
    int n,x;
    scanf("%d%d",&n,&x);
    int i;
    int s[1200]= {0};
    int a,b,j,tt;
    for(i=0; i<n; i++)
    {
        scanf("%d%d",&a,&b);
        if(a>b)
        {
            tt=a;
            a=b;
            b=tt;

        }
        for(j=a; j<=b; j++)
        {
            s[j]++;
        }
    }
    int l=-1,r=-1;
    for(i=0; i<1001; i++)
    {
        if(s[i]==n)
        {
            l=i;
            break;
        }
    }
    for(i=1000; i>=0; i--)
    {
        if(s[i]==n)
        {
            r=i;
            break;
        }
    }
    int ans;
    if(l==-1)
        ans=-1;
    else
    {
        if(x>=l&&x<=r)
        {
            ans=0;
        }
        else
        {
            ans=min(abs(x-l),abs(x-r));
        }
    }
    printf("%d\n",ans);

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值