codeforce830A. Office Keys
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.
Examples
input
2 4 50
20 100
60 10 40 80
output
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 50seconds. Thus, after 50 seconds everybody is in office with keys.
题意
一条数轴,n个人,k把钥匙, 和一间在 p 的屋子。每个人拿上
每个人和每把 key 都有个坐标.,每个人每秒能走一单位的距离。每个人都要拿一把 key,,然后走进屋子。俩人不能拿同一把 key,求所有人都走到屋子的最小时间。
分析
首先当然是拿离自己近的钥匙,所以可以先把钥匙和人按坐标排序,然后要求所有人走进屋子的时间最少,显然可以二分。
所以先二分时间,判断是否满足即可。
code
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream> using namespace std; const int MAXN = ;
typedef long long LL; LL r[MAXN],k[MAXN];
bool vis[MAXN];
LL n,m,pos; int read()
{
int x = ,f = ;char ch = getchar();
while (ch<''||ch>'') {if (ch=='-') f = -; ch = getchar(); }
while (ch>=''&&ch<='') {x=(x<<)+(x<<)+ch-''; ch = getchar(); }
return x*f;
}
bool check(LL x)
{
int cnt = ;
memset(vis,,sizeof(vis));
for (int i=; i<=n; ++i)
for (int j=; j<=m; ++j)//人与钥匙都是从左边枚举
{
if (vis[j]) continue ;//钥匙已经拿了
if (abs(r[i]-k[j])+abs(k[j]-pos)<=x)//满足条件
{
vis[j] = true;
cnt++;
break; //只要有一个即满足条件,break
}
}
if (cnt==n) return true;
return false;
}
int main()
{ n = read();m = read();pos = read();
for (int i=; i<=n; ++i)
r[i] = read();
for (int i=; i<=m; ++i)
k[i] = read(); sort(r+,r+n+);
sort(k+,k+m+);
LL mid,l = ,r = 1e10,ans; while (l<=r)//二分时间
{
mid = (l+r)>>;
if (check(mid))
{
ans = mid;
r = mid-;
}
else l = mid+;
}
cout<<ans;
return ;
}
codeforce830A. Office Keys的更多相关文章
- 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, 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 ...
- 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 ...
- CF-831D Office Keys 思维题
http://codeforces.com/contest/831/problem/D 题目大意是在一条坐标轴上,给出n个人,k把钥匙(k>=n)以及终点的坐标,所有人都可以同时运动,但不可以公 ...
- AC日记——830A - Office Keys
思路: 背包: 代码: #include <cmath> #include <cstdio> #include <cstring> #include <ios ...
- 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 ...
随机推荐
- js之BOM和DOM
今天我们来学习js中的一些基础的操作. 一.BOM对象 BOM(浏览器对象模型),可以对浏览器窗口进行访问和操作.使用 BOM,开发者可以移动窗口.改变状态栏中的文本以及执行其他与页面内容不直接相 ...
- CF1152C Neko does Maths
思路: 假设a <= b,lcm(a + k, b + k) = (a + k) * (b + k) / gcd(a + k, b + k) = (a + k) * (b + k) / gcd( ...
- <转载>为什么VR不可能成功?
这是一个来自Quora的回答,我把要点总结翻译了下,供大家参考批判. How big and issue the nausea problem for Virtual Reality products ...
- zabbix-2.2.2(Ubuntu 14.04 LTS/OpenLogic 7.2)
平台: arm 类型: ARM 模板 软件包: apache-2.4.7 mariadb-5.5.50 mysql-5.5.52-0ubuntu0.14.04.1 php-5.4.16 php-5.5 ...
- 阿里 EasyExcel 7 行代码优雅地实现 Excel 文件生成&下载功能
欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...
- Android商城开发系列(十二)—— 首页推荐布局实现
首页新品推荐的布局效果如下图: 这块布局是使用LinearLayout和GridView去实现,新建recommend_item.xml,代码如下所示: <?xml version=" ...
- 利用jsplumb和碰撞检测自动生成流程图
使用jsplumb构建流程图模型时,有一个需求要求,选项可以从选项表中拖拽到指定容器,并且两个选项要接触到的时候才能连接起来,不接触不能连接.效果图如下 略丑- 因为这里用到了拖拽,拖放功能,所以用到 ...
- php使用GD库实现图片水印和缩略图——给图片添加文字水印
今天呢,就来学习一下在php中使用PD库来实现对图片水印的文字水印方法,不需要PS哦! 首先,准备素材 (1)准备一张图片 (2)准备一张水印(最好是透明的,即背景是白色底) (3)准备一中字体(在电 ...
- webpack打包过程如何调试?
本文适用于已经会使用webpack的前端开发人员,但是想进一步了解webpack细节和进阶. 首先请读者按照我前一篇文章 Webpack 10分钟入门介绍的步骤,在本地搭建一个webpack的hell ...
- 一个SAP顾问在美国的这些年
今天的文章来自我的老乡宋浩,之前作为SAP顾问在美国工作多年.如今即将加入SAP成都研究院S4CRM开发团队.我们都是大邑人. 大邑县隶属于四川省成都市,位于成都平原西部,与邛崃山脉接壤.东与崇州市交 ...