CF 14B B. Young Photographer

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 integersai,?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 test(s)

input

3 3
0 7
14 2
4 6

output

1

我的做法维护区间最大值(maxn)和最小值(minn)=-=,最后还要判一下x0在不在这个区间内;

如果在,不用动,输出0,不再则输出到maxn,minn的最小值。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,x0;
struct node{
    int a,b;
}s[110];
int cmp(node l1,node l2)
{
    return l1.a<l2.a;
}
int main()
{
    int u,v,i;
    while(cin>>n>>x0)
    {
        for(i=0;i<n;i++)
        {
            cin>>u>>v;
            if(u>v)
                swap(u,v);
            s[i].a=u;
            s[i].b=v;
        }
        sort(s,s+n,cmp);
        int maxn=s[0].b;
        int minn=s[0].a;
        for(i=1;i<n;i++)
        {
            if(s[i].b<minn)
                break;
            if(s[i].a>maxn)
                break;
            if(s[i].a>minn)
                minn=s[i].a;
            if(s[i].b<maxn)
                maxn=s[i].b;
        }
        if(i==n)
        {
            if(x0>=minn&&x0<=maxn)
                cout<<0<<endl;
            else
                cout<<min(abs(minn-x0),abs(maxn-x0))<<endl;
        }
        else
            cout<<-1<<endl;
    }
    return 0;
}

CF 14B B. Young Photographer

时间: 2024-12-10 07:12:13

CF 14B B. Young Photographer的相关文章

CodeForces 14B Young Photographer

题目链接:http://codeforces.com/problemset/problem/14/B 题意:一条一维坐标轴,一个拍摄者在x0的位置,如果他在一个运动员运动的周期范围内(a1~b1),他就可以排到这个运动员,问你最小走几步的位置可以拍到所有运动员,若没有输出-1.(a可能大于b) 数据范围:运动员人数是不大于100,位置是0~1000 分析:暴力枚举1000个点在所有ab范围内并且离x0最近的,若没有就输出-1. 代码: #include<cstdio> #include<

[2016-03-22][CF][69A][Young Physicist]

时间:2016-03-22 19:41:34 星期二 题目编号:[2016-03-22][CF][69A][Young Physicist] 题目大意:判断向量和是否为0 分析:对应坐标相加 遇到的问题:不能用x+y+z来判断是否都为0,除非输入都是正数 #include <cstdio> using namespace std; int main(){ int a,b,c,x,y,z,n; x = y = z = 0; scanf("%d",&n); for(in

【打CF,学算法——二星级】CodeForces 237B Young Table (构造)

[CF简介] 提交链接:CF 237B 题面: B. Young Table time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You've got table a, consisting of n rows, numbered from 1 to n. The i-th line of table a contains ci

微信 {&quot;errcode&quot;:40029,&quot;errmsg&quot;:&quot;invalid code, hints: [ req_id: Cf.y.a0389s108 ]&quot;}

{"errcode":40029,"errmsg":"invalid code, hints: [ req_id: Cf.y.a0389s108 ]"} 问题:微信网页授权后,获取到 openid 了,一刷新又没了 微信网页授权获取到的 code 只能使用一次(5分钟内有效),使用一次后,马上失效. 页面授权跳转成功,根据 code 也换取到 openid 了. 此时刷新页面,并不会再次进行授权,而是直接刷新了一下上一次授权跳转后的链接,带的还是

CF with friends and user&#39;s influence considered on NYC data(updated Aug,11st)

Here is the code link: https://github.com/FassyGit/LightFM_liu/blob/master/U_F1.py I use NYC data as other experimens. The split of the training data was seperated by the timeline, and I have normalised the interaction matrix by replacing the checkin

Young Maids

E - Young Maids Time limit : 2sec / Memory limit : 256MB Score : 800 points Problem Statement Let N be a positive even number. We have a permutation of (1,2,…,N), p=(p1,p2,…,pN). Snuke is constructing another permutation of (1,2,…,N), q, following th

CF 750

今天CF打的块残废了     就是一废物 A 在24点之前到 直接模拟即可 #include<stdio.h> #include<algorithm> #include<cstring> #include<string> #include<cmath> using namespace std; #define LL long long #define MAXN 1010 #define inf 1000000000.0 int main() {

CF #394 (2) 5/6

Codeforces Round #394 (Div. 2) 总结:有毒的一场比赛.做了三题,结果A被叉,B.C挂综测,还hack失败一发,第一次在CF体会到了-50分的感觉..不知道是不是人品好,比赛时room炸了,然后,unrated.. A  水题,判一下0 0,然后abs(a-b)<=1 B  水题,组个间距比较一下,但一个数的时候要判一下 C  直接暴力上的题 D  也是xjb暴力 题意:给出n,l,r, a[], p[],另有两个数组b[], c[],ci=bi-ai.l<=ai,

一场CF的台前幕后(上)——转

前奏 大约4月份的时候,业界毒瘤pyx噔噔噔跑过来说:“酷爱!我YY了一道题!准备当CF的C” 我当时就被吓傻了."Yet another Chinese round?" “区间取模,区间求和” 感觉这题还不错?不过pyx嫌水了…… 好办!当时我刚刚出完动态仙人掌不久,于是一拍脑袋说:把这个问题出到仙人掌上去! 当然被pyx鄙视了…… 后来一直就没啥动静,直到5月底的CTSC. 试机的时候pyx给我看了套他出的神题……里面有一道题……我不小心读成了下面这个样子: “给定n个m维的模2意