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. .net MVC简洁的登录页面

    初学mvc,参考别人的代码写的 界面效果如下: 代码如下: @{ Layout = null; } <!DOCTYPE html> <html> <head> &l ...

  2. C语言之标准头文件模板

    /*======================================================================================* * 版权 : xxx ...

  3. Java对象回收流程

    一.可回收对象判断 引用计数器算法:为对象放置一个引用计数器,当对象被引用时则计数器加一,如果一个对象的计数器标识为零的时候,则表明该对象可被回收.这种方法比较简单,但无法解决对象之间互相引用的情况. ...

  4. Linux运维精华面试题

    1.什么是运维?什么是游戏运维? 1)运维是指大型组织已经建立好的网络软硬件的维护,就是要保证业务的上线与运作的正常,在他运转的过程中,对他进行维护,他集合了网络.系统.数据库.开发.安全.监控于一身 ...

  5. 针对特定网站scrapy爬虫的性能优化

    在使用scrapy爬虫做性能优化时,一定要根据不同网站的特点来进行优化,不要使用一种固定的模式去爬取一个网站,这个是真理,以下是对58同城的爬取优化策略: 一.先来分析一下影响scrapy性能的set ...

  6. 19-04【mysql】transaction,autocommit

    本地使用mysql测试结果如下, 没有使用start transaction的情况下: 如果set autocommit=1,则每一条语句默认提交: 如果set autocommit=0,则这条语句默 ...

  7. JVM学习02:GC垃圾回收和内存分配

    JVM学习02:GC垃圾回收和内存分配 写在前面:本系列分享主要参考资料是  周志明老师的<深入理解Java虚拟机>第二版. GC垃圾回收和内存分配知识要点Xmind梳理 案例分析1-(G ...

  8. pom.xml文件释义

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

  9. 阅读笔记:JAVA - chapter 1 & 2

    static 即使没有创建对象,也能调用这个方法. 当Static method, static变量有定义,不同对象将指向同一存储空间,将其初始化. 存储方式 寄存器 堆栈:对象引用,需要知道存活多久 ...

  10. JavaSE基础知识(5)—面向对象(5.7 final关键字)

    一.说明 final属于一种修饰符,可以用于修饰类和属性.方法.局部变量 二.特点 1.修饰类 该类不能被继承,如String.Integer等 2.修饰方法 该方法不能被重写 3.修饰变量(属性和局 ...