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.

——————————————————————————————————
题目的意思是在一条直线上给出n个人的位置和m把钥匙的位置和一个办公室位置,求每个人拿了钥匙进办公室最少时间,每把钥匙只能被一个人拿
思路:先排序,再二分时间,验证能否在这个时间都进办公室,验证时可以贪心尽可能让每个人拿左边钥匙
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset> using namespace std; #define LL long long
const int INF = 0x3f3f3f3f;
#define MAXN 2000010 LL a[100005],b[100005];
LL p;
int n,m;
bool ok(LL mid)
{
int l=0,r=0;
while(l<n&&r<m)
{
if(fabs(a[l]-b[r])+fabs(p-b[r])<=mid)
l++,r++;
else
r++;
}
if(l==n) return 1;
return 0;
} int main()
{ scanf("%d%d%lld",&n,&m,&p);
for(int i=0; i<n; i++)
scanf("%lld",&a[i]);
for(int j=0; j<m; j++)
scanf("%lld",&b[j]);
sort(a,a+n);
sort(b,b+m);
LL l=0,r=100000000000;
LL ans;
while(l<=r)
{
LL mid=(l+r)/2;
if(ok(mid)) r=mid-1,ans=mid;
else l=mid+1;
}
printf("%lld\n",ans);
return 0;
}

  

Codeforces831D Office Keys的更多相关文章

  1. CF830A Office Keys(贪心)

    CF830A Office Keys [题目链接]CF830A Office Keys [题目类型]贪心 &题意: 有n个人,k个钥匙,一个目的地,求让n个人都回到目的地的最短时间,每个人都要 ...

  2. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) D. Office Keys time limit per test2 seconds 二分

    D. Office Keys time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  3. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Office Keys(思维)

    Office Keys time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  4. codeforce830A. Office Keys

    A. Office Keys time limit per test: 2 seconds memory limit per test: 256 megabytes input standard: i ...

  5. code force 424 A - Office Keys

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

  6. CF-831D Office Keys 思维题

    http://codeforces.com/contest/831/problem/D 题目大意是在一条坐标轴上,给出n个人,k把钥匙(k>=n)以及终点的坐标,所有人都可以同时运动,但不可以公 ...

  7. AC日记——830A - Office Keys

    思路: 背包: 代码: #include <cmath> #include <cstdio> #include <cstring> #include <ios ...

  8. Codeforces VK Cup Finals #424 Div.1 A. Office Keys(DP)

    显然是不可能交叉取钥匙的,于是把钥匙和人都按坐标排序就可以DP了 钥匙可以不被取,于是f[i][j]表示前i个钥匙被j个人拿的时间 f[i][j]=min(f[i-1][j],max(f[i-1][j ...

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

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

随机推荐

  1. java编译带中文是显示乱码的错误

    FirstJava.java:3: 错误: 编码GBK的不可映射字符 System.out.println("娆㈣繋瀛︿範Java绋嬪簭锛?")锛? ^FirstJava.java ...

  2. HTML5智能表单

    HTML5 智能表单 1.表单新增属性  ☀ autofocus 属性 <input type="text" autofocus/>设置 autofocus 属性,使文 ...

  3. Exp4 恶意代码分析 20165110

    Exp4 恶意代码分析 20165110 一.实践目标 1.是监控你自己系统的运行状态,看有没有可疑的程序在运行. 2.是分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分析工具尽量使用原生 ...

  4. MTD的认识

    MTD即内存计数设备,是linux中对ROM.NORFLASH.NAND  Flash等存储设备抽象出来的一个设备层,它向上提供统一的访问接口:读写.擦除等:屏蔽了底层硬件的操作.各类存储设备的差别. ...

  5. JVM性能优化读后笔记

    java性能优化权威指南读后笔记 三重境界 1.花似雾中看:对于遇到的额问题还看不清,不知道真真假假,是是非非. 2.悠然见南山:虽然刚开始对这个领域还不清楚,但随着时间推移,你对它有许多自己的见解, ...

  6. Ubuntu 16.04 安装OpenCV 3.4.3

    cmake过程中可能遇到的问题:1.如果网络不好,出现ippicv_linux_20151201.tgz无法在终端下载的情况,则可以先单独下载 ippicv_linux_20151201.tgz之后, ...

  7. pom.xml文件释义

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  8. ubuntu16.04安装Navicate

    1.   http://download2.navicat.com/download/navicat100_mysql_en.tar.gz 2.   tar -zxvf  /home/rain/dow ...

  9. Entity Framework连接postgresql,code first

    官方介绍地址 http://www.npgsql.org/ef6/index.html 首先创建一个控制台应用程序 ,本例居于.NETFramework,Version=v4.6.1 安装包 Enti ...

  10. 【aardio】回车换行符

    回车换行符 在计算机还没有出现之前,有一种叫做电传打字机(Teletype Model 33)的玩意,每秒钟可以打10个字符.但是它有一个问题,就是打完一行换行的时候,要用去0.2秒,正好可以打两个字 ...