D. Office Keys
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.

You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.

Input

The first line contains three integers nk and p (1 ≤ n ≤ 1 000, n ≤ k ≤ 2 000, 1 ≤ p ≤ 109) — the number of people, the number of keys and the office location.

The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — positions in which people are located initially. The positions are given in arbitrary order.

The third line contains k distinct integers b1, b2, ..., bk (1 ≤ bj ≤ 109) — positions of the keys. The positions are given in arbitrary order.

Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.

Output

Print the minimum time (in seconds) needed for all n to reach the office with keys.

Examples
input
2 4 50
20 100
60 10 40 80
output
50
input
1 2 10
11
15 7
output
7
Note

In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50seconds. Thus, after 50 seconds everybody is in office with keys.

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e5+,M=2e6+,inf=1e9+;
const LL INF=1e18+,mod=1e9+; int n,k;
LL a[N],b[N],p;
LL dis(LL s,LL mid,LL e)
{
return abs(mid-s)+abs(e-mid);
}
int check(LL x)
{
int s=,flag=;
for(int i=;i<=n;i++)
{
while(s<=k&&dis(a[i],b[s],p)>x)s++;
if(s>k){
flag=;
break;
}
s++;
}
if(!flag)return ;
return ;
}
int main()
{
scanf("%d%d%lld",&n,&k,&p);
for(int i=;i<=n;i++)
scanf("%lld",&a[i]);
for(int i=;i<=k;i++)
scanf("%lld",&b[i]);
sort(a+,a++n);
sort(b+,b++k);
LL s=,e=1e10,ans=-;
while(s<=e)
{
LL mid=(s+e)>>;
if(check(mid))
{
ans=mid;
e=mid-;
}
else s=mid+;
}
printf("%lld\n",ans);
return ;
}

Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) D. Office Keys time limit per test2 seconds 二分的更多相关文章

  1. 【推导】Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals) A. Office Keys

    选择的钥匙一定是连续的,人和钥匙一定从左到右连续对应. 就枚举钥匙区间即可. #include<cstdio> #include<algorithm> using namesp ...

  2. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)

    http://codeforces.com/contest/831 A. Unimodal Array time limit per test 1 second memory limit per te ...

  3. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) A 水 B stl C stl D 暴力 E 树状数组

    A. Unimodal Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  4. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划

    There are n people and k keys on a straight line. Every person wants to get to the office which is l ...

  5. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem C (Codeforces 831C) - 暴力 - 二分法

    Polycarp watched TV-show where k jury members one by one rated a participant by adding him a certain ...

  6. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)A,B,C

    A:链接:http://codeforces.com/contest/831/problem/A 解题思路: 从前往后分别统计递增,相等,递减序列的长度,如果最后长度和原序列长度相等那么就输出yes: ...

  7. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem F (Codeforces 831F) - 数论 - 暴力

    题目传送门 传送门I 传送门II 传送门III 题目大意 求一个满足$d\sum_{i = 1}^{n} \left \lceil \frac{a_i}{d} \right \rceil - \sum ...

  8. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 831E) - 线段树 - 树状数组

    Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this int ...

  9. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem A - B

    Array of integers is unimodal, if: it is strictly increasing in the beginning; after that it is cons ...

随机推荐

  1. <转>jmeter(十三)常见问题及解决方法

    本博客转载自:http://www.cnblogs.com/imyalost/category/846346.html 个人感觉不错,对jmeter讲解非常详细,担心以后找不到了,所以转发出来,留着慢 ...

  2. Python+OpenCV图像处理(八)—— 图像直方图

    直方图简介:图像的直方图是用来表现图像中亮度分布的直方图,给出的是图像中某个亮度或者某个范围亮度下共有几个像素.还不明白?就是统计一幅图某个亮度像素数量.比如对于灰度值12,一幅图里面有2000 个像 ...

  3. js中使用0 “” null undefined {}需要注意

    注意:在js中0为空(false) ,代表空的还有“”,null ,undefined: 如果做判断if(!上面的四种值):返回均为false console.log(!null);// true c ...

  4. TCP/IP协议三次握手与四次挥手

    一.标志位和序号 seq序号 :发送方随机生成的 ack确认序号:ack=seq+1 标志位ACK=1时确认序号有效 SYN标志位:发起一个新连接 ACK标志位:确认序号有效 FIN标志位:断开连接 ...

  5. Java开发万年历

    自己做出来的万年历: 以下代码: public class Test2 { public static void main(String[] args) {  Scanner sc = new Sca ...

  6. Always an integer UVALive - 4119

    题目很简单,就是求表达式(P/D)的结果是不是整数.其中P是一个整系数的多项式,D是一个正整数. 把1-k(最高次)+1都试一次就好了.结论可以总结归纳得到.(k取 0, 1, 2 .... 的情况推 ...

  7. mysql5.5被django抛弃,安装mysql5.7记录

    安装: https://www.jb51.net/article/123004.htm 问题解决: https://blog.csdn.net/zztingfeng/article/details/8 ...

  8. SQL语句的优化方法

    减少对数据库的查询次数 尽量使用相同的或非常类似的SQL语句进行查询 避免不带任何条件的SQL语句的执行 sql语句用大写 别名的使用(1.5倍)

  9. c++实现“扫描检测硬件改动”

    这里需要用到cfgmgr32.h,参考了网上好几篇博文. #include <windows.h> #include <stdio.h> #include <cfgmgr ...

  10. linux释放页面缓存drop_caches

    关于drop_caches文件:系统默认为0 在Documentation/sysctl/vm.txt中有如下描述: drop_caches Writing to this will cause th ...