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 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 n, k 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.
Example
2 4 50
20 100
60 10 40 80
50
1 2 10
11
15 7
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.
/*
二分答案
*/
#include <bits/stdc++.h> #define MAXN 1005
#define MAXM 2005
#define LL long long
#define INF 2000000007
using namespace std; int n;
LL k,p;
LL a[MAXN],b[MAXM]; bool ok(LL x){
LL pos=-;
for(int i=;i<n;i++){
while(pos<k){
pos++;
if(abs(a[i]-b[pos])+abs(b[pos]-p)<=x) break;
}
if(pos>=k) return false;
}
return true;
} int main(int argc, char *argv[]){
//freopen("in.txt","r",stdin);
scanf("%d%I64d%I64d",&n,&k,&p);
for(int i=;i<n;i++){
scanf("%I64d",&a[i]);
}
for(int i=;i<k;i++){
scanf("%I64d",&b[i]);
}
sort(a,a+n);
sort(b,b+k);
LL l=,r=INF;
LL res=;
while(l<=r){
LL m=(l+r)/;
if(ok(m)){
res=m;
r=m-;
}else{
l=m+;
}
}
printf("%I64d\n",res);
return ;
}
code force 424 A - Office Keys的更多相关文章
- 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 ...
- 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 ...
- codeforce830A. Office Keys
A. Office Keys time limit per test: 2 seconds memory limit per test: 256 megabytes input standard: i ...
- CF830A Office Keys(贪心)
CF830A Office Keys [题目链接]CF830A Office Keys [题目类型]贪心 &题意: 有n个人,k个钥匙,一个目的地,求让n个人都回到目的地的最短时间,每个人都要 ...
- Codeforces831D Office Keys
D. Office Keys time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- 【Codeforces Round #424 (Div. 2) D】Office Keys
[Link]:http://codeforces.com/contest/831/problem/D [Description] 有n个人,它们都要去一个终点,终点位于p; 但是,在去终点之前,他们都 ...
- 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 ...
- 【推导】Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals) A. Office Keys
选择的钥匙一定是连续的,人和钥匙一定从左到右连续对应. 就枚举钥匙区间即可. #include<cstdio> #include<algorithm> using namesp ...
- A. Office Keys ( Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals) )
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...
随机推荐
- SharedPreferences的使用
- 记录一次无聊的(经历了Nodejs -> Shell -> C)的探索问题过程
提出问题 在运行项目的服务器的git是1.8.3.1版本的时候,pm2 deploy 项目,服务器fetch不到最新的一次commit. 对于这个问题,在pm2的github也有issues讨论.然后 ...
- CentOS7安装后配置MariaDB
安装后,优先推荐先对安全设置进行配置,键入命令 sudo mysql_secure_installation 键入当前密码,当前没有,直接回车,之后跟随提示会问几个问题:设置 root 密码? / 移 ...
- 防止html5的video标签在iphone中自动全屏
问题: 当在iphone中打开html5页面中的video视频时,会默认调取系统播放器,全屏播放视频资源. 解决方式: 1. 首先在html5页面的video标签中添加webkit-playsinli ...
- H5上传图片并使用canvas制作海报
马上就要"十一"国庆节了,又恰逢公司已经三周岁了,所以市场部和产品共同策划了一个"正青春,共成长"的主题代言活动,准备在国庆节以及中秋节期间让公司员工和用户为公 ...
- TOP命令详解(负载情况)
简介 top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. top显示系统当前的进程和其他状况,是一个动态显示过程,即可以通过用户按 ...
- http content-type accept的区别
1.Accept属于请求头, Content-Type属于实体头. Http报头分为通用报头,请求报头,响应报头和实体报头. 请求方的http报头结构:通用报头|请求报头|实体报头 响应方的http报 ...
- Twitter的分布式系统中ID生成方法——Snowflake
Twitter-Snowflake算法产生的背景相当简单,为了满足Twitter每秒上万条消息的请求,每条消息都必须分配一条唯一的id,这些id还需要一些大致的顺序(方便客户端排序),并且在分布式系统 ...
- GBDT(MART)概念简介
GBDT(Gradient Boosting Decision Tree) 又叫 MART(Multiple Additive Regression Tree),是一种用于回归的机器学习算法,该算法由 ...
- 自测-4 Have Fun with Numbers
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, wit ...