CodeForces - 831D ---Office Keys

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 50 seconds. Thus, after 50 seconds everybody is in office with keys.

题意:在一条直线上, 给你n个人的位置,m把钥匙的位置,和一个办公室的位置;

让你求每个人只有拿到一把钥匙才能进入办公室,每个人进入办公室的时间等于他拿到钥匙的时间加上拿到钥匙后走到办公室的时间;没把要是只能拿一次;求全部人拿到要是的最短时间;

 

思路:一维坐标,求最短时间也就是最短距离,首先想到二分,重点是二分什么;

我们选择的值二分最短时间,那就要保证每个人都能进入办公室,并且人拿到钥匙的时间+拿到钥匙后走到办公室的使劲按要 <= 最短时间;看代码的注释把;

需要注意的是 L和R你需要开LL型的, 因为最大的时候 mid会爆int,

当check(mid)== false 的时候l = mid + 1, 然后l +r = 1e9 + 2e9就爆int了

ac码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
typedef long long LL;
const int maxn = 3e5 + 19;
LL n , m, k;
LL a[maxn], b[maxn];

bool check(int x)
{
	LL now = 1;     
	for(LL i = 1;i <= m;i++)   
	{
		if(abs(b[i] - a[now]) > x) continue;   //当前钥匙的位置 - 第now个人的距离大于你二分进来的x  跳过
		else if(abs(b[i] - a[now]) + abs(k - b[i]) <= x) now++;  //第now个人的距离到钥匙的距离 + 拿到钥匙到办公室的距离小于 x  就now++判断下一个人
	}
	if(now > n) return true;  //如果n个人都拿到钥匙进入办公室了
	else return false;
}

int main()
{
	scanf("%lld %lld %lld",&n, &m, &k);
	int ans = 0;
	for(LL i = 1;i <= n;i++) scanf("%lld", &a[i]);  //读入人的位置
	for(LL i = 1;i <= m;i++) scanf("%lld", &b[i]);  //读入钥匙的位置
	sort(a + 1,a + 1 + n);
	sort(b + 1,b + 1 + m);
	LL l = 0;
	LL r = 2e9 + 19;
	while(l <= r) //二分
	{
		LL mid = (l + r) / 2;
		if(check(mid))
		{
			ans = mid;
			r = mid - 1;
		}
		else l = mid  + 1;
	}
	printf("%d\n", ans);
	return 0;
}

 

你可能感兴趣的:(ACM日常写题记录)