博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
codeforce830A. Office Keys
阅读量:5164 次
发布时间:2019-06-13

本文共 3593 字,大约阅读时间需要 11 分钟。

A. 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.

题意

一条数轴,n个人,k把钥匙, 和一间在 p 的屋子。每个人拿上

每个人和每把 key 都有个坐标.,每个人每秒能走一单位的距离。每个人都要拿一把 key,,然后走进屋子。俩人不能拿同一把 key,求所有人都走到屋子的最小时间。

分析

首先当然是拿离自己近的钥匙,所以可以先把钥匙和人按坐标排序,然后要求所有人走进屋子的时间最少,显然可以二分。

所以先二分时间,判断是否满足即可。

code

1 #include
2 #include
3 #include
4 #include
5 #include
6 7 using namespace std; 8 9 const int MAXN = 2010;10 typedef long long LL;11 12 LL r[MAXN],k[MAXN];13 bool vis[MAXN];14 LL n,m,pos;15 16 int read()17 {18 int x = 0,f = 1;char ch = getchar();19 while (ch<'0'||ch>'9') {
if (ch=='-') f = -1; ch = getchar(); }20 while (ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+ch-'0'; ch = getchar(); }21 return x*f;22 }23 bool check(LL x)24 {25 int cnt = 0;26 memset(vis,0,sizeof(vis));27 for (int i=1; i<=n; ++i)28 for (int j=1; j<=m; ++j)//人与钥匙都是从左边枚举 29 {30 if (vis[j]) continue ;//钥匙已经拿了 31 if (abs(r[i]-k[j])+abs(k[j]-pos)<=x)//满足条件 32 {33 vis[j] = true;34 cnt++;35 break; //只要有一个即满足条件,break 36 }37 }38 if (cnt==n) return true;39 return false;40 }41 int main()42 {43 44 n = read();m = read();pos = read();45 for (int i=1; i<=n; ++i)46 r[i] = read();47 for (int i=1; i<=m; ++i) 48 k[i] = read();49 50 sort(r+1,r+n+1);51 sort(k+1,k+m+1);52 LL mid,l = 0,r = 1e10,ans;53 54 while (l<=r)//二分时间 55 {56 mid = (l+r)>>1;57 if (check(mid))58 {59 ans = mid;60 r = mid-1;61 }62 else l = mid+1;63 }64 cout<

 

转载于:https://www.cnblogs.com/mjtcn/p/7323930.html

你可能感兴趣的文章
(转)matlab练习程序(HOG方向梯度直方图)
查看>>
『Raid 平面最近点对』
查看>>
【ADO.NET基础-数据加密】第一篇(加密解密篇)
查看>>
C语言基础小结(一)
查看>>
STL中的优先级队列priority_queue
查看>>
UE4 使用UGM制作血条
查看>>
浏览器对属性兼容性支持力度查询网址
查看>>
OO学习总结与体会
查看>>
虚拟机长时间不关造成的问题
查看>>
面试整理:Python基础
查看>>
Python核心编程——多线程threading和队列
查看>>
Program exited with code **** 相关解释
查看>>
植物大战僵尸中文年度版
查看>>
26、linux 几个C函数,nanosleep,lstat,unlink
查看>>
投标项目的脚本练习2
查看>>
201521123107 《Java程序设计》第9周学习总结
查看>>
Caroline--chochukmo
查看>>
iOS之文本属性Attributes的使用
查看>>
从.Net版本演变看String和StringBuilder性能之争
查看>>
Excel操作 Microsoft.Office.Interop.Excel.dll的使用
查看>>