B. Young Photographer time limit per test 2 seconds memory limit per test 64 megabytes input standard input output standard outputadmin管理员组文章数量:1532656
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.
InputThe first line of the input file contains integers n and x0 (1 ≤ n ≤ 100; 0 ≤ x0 ≤ 1000). The following n lines contain pairs of integersai, bi (0 ≤ ai, bi ≤ 1000; ai ≠ bi).
OutputOutput the required minimum distance in the same units as the positions on the racetrack. If there is no such a position, output -1.
Examples input3 3 0 7 14 2 4 6output
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;
}
本文标签: youngPhotographer
版权声明:本文标题:14B - Young Photographer 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1726363771a1067194.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论