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

Codeforces Beta Round #14 (Div. 2) B. Young Photographer 水题的更多相关文章

  1. Codeforces Beta Round #14 (Div. 2) C. Four Segments 水题

    C. Four Segments 题目连接: http://codeforces.com/contest/14/problem/C Description Several months later A ...

  2. Codeforces Beta Round #4 (Div. 2 Only) A. Watermelon 水题

    A. Watermelon 题目连接: http://www.codeforces.com/contest/4/problem/A Description One hot summer day Pet ...

  3. Codeforces Beta Round #6 (Div. 2 Only) A. Triangle 水题

    A. Triangle 题目连接: http://codeforces.com/contest/6/problem/A Description Johnny has a younger sister ...

  4. Codeforces Beta Round #14 (Div. 2)

    Codeforces Beta Round #14 (Div. 2) http://codeforces.com/contest/14 A 找最大最小的行列值即可 #include<bits/s ...

  5. Codeforces Beta Round #14 (Div. 2) D. Two Paths 树形dp

    D. Two Paths 题目连接: http://codeforces.com/contest/14/problem/D Description As you know, Bob's brother ...

  6. Codeforces Beta Round #14 (Div. 2) A. Letter 水题

    A. Letter 题目连接: http://www.codeforces.com/contest/14/problem/A Description A boy Bob likes to draw. ...

  7. Codeforces Beta Round #14 (Div. 2) D. Two Paths 树的直径

    题目链接: http://codeforces.com/contest/14/problem/D D. Two Paths time limit per test2 secondsmemory lim ...

  8. Codeforces Beta Round #14 (Div. 2) Two Paths (树形DP)

    Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input outp ...

  9. TTTTTTTTTTTTT 树的直径 Codeforces Beta Round #14 (Div. 2) D. Two Paths

    tiyi:给你n个节点和n-1条边(无环),求在这个图中找到 两条路径,两路径不相交,求能找的两条路径的长度的乘积最大值: #include <iostream> #include < ...

随机推荐

  1. 并查集:POJ 1182 食物链 复习

    #include <iostream> #include <algorithm> #include <cstring> #include <cstdlib&g ...

  2. 《翻译》PEP 380 – 委托子生成器语法

     PEP 380 – 委托子生成器语法 翻译自: https://www.python.org/dev/peps/pep-0380/ 摘要 一项新的语法被提出了:生成器委托其部分操作给另一个生成器.委 ...

  3. 20155314 2016-2017-2 《Java程序设计》第7周学习总结

    20155314 2016-2017-2 <Java程序设计>第7周学习总结 教材学习内容总结 了解Lambda语法 了解方法引用 了解Fucntional与Stream API 掌握Da ...

  4. 转载 你不知道的super

    http://funhacks.net/2016/11/09/super/ super仅被用于新式类 在类的继承中,如果重定义某个方法,该方法会覆盖父类的同名方法,但有时,我们希望能同时实现父类的功能 ...

  5. G. (Zero XOR Subset)-less(线性基)

    题目链接:http://codeforces.com/contest/1101/problem/G 题目大意:给你n个数,然后让你把这n个数分成尽可能多的集合,要求,每个集合的值看做这个集合所有元素的 ...

  6. requests(二): json请求中固定键名顺序&消除键和值之间的空格

    继上一篇requests发送json请求的文章后,实际工作中遇到了以下2种情况. 1:服务端要求json字符串,键名的顺序固定  2.服务端对于接收到的json数据中,若key和value之间有空格, ...

  7. SANS社区帐号邮件激活问题

    注册时,密码需要数字,大写字母,小写字母,符号10位以上才能注册成功    吐槽:谁来爆破一下这种强度的密码,哈哈. 在我的文章中,有 计算机取证 分类,里面的一篇文章 Virtual Worksta ...

  8. Explain EV in /proc/bus/input/devices data【转】

    转自:https://unix.stackexchange.com/questions/74903/explain-ev-in-proc-bus-input-devices-data It repre ...

  9. cefSharp获取百度搜索结果页面的源码

    using CefSharp; using CefSharp.WinForms; using System; using System.Collections.Generic; using Syste ...

  10. 文字小于12px时,设置line-height不居中问题

    设置了文字了小于12px时,会存在设置了line-height的不生效的问题,主要是由于基线的问题,这篇文章解释的很清楚,有兴趣的可以看下https://blog.csdn.net/q12151634 ...