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);
}