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 ...
随机推荐
- codevs 1664 清凉冷水
1664 清凉冷水 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 闷热的夏天,威斯康辛州的奶制品地区提供冷水供奶牛 ...
- JAVA中的多态概念
多态性是指同一操作作用于某一类对象,可以有不同的解释,产生不同的执行结果. 多态存在的三个必要条件 需要存在继承和实现关系 同样的方法调用而执行不同操作,运行不同代码. 在运行时父类或者接口的引用变量 ...
- 常用CSS3属性整理
常用CSS3属性整理 文本 文本超出部分折叠 white-space:nowarp; overflow:hidden; text-overflow:ellipsis word-warp 边界换行 no ...
- 约瑟夫环问题及PHP代码实现
php实现猴子选大王 <?php /** * @param $n 猴子数量 * @param $m 出列的那个数 */ function king($n,$m){ $monkeys = rang ...
- CAsyncSocket create创建套接字失败
解决方法: 在继承CAsyncSocket 类的子类的构造函数中加入以下代码: if (!AfxSocketInit()) AfxMessageBox(IDP_SOCKETS_INIT_FAILED) ...
- 使用Java程序消费SAP Leonardo的机器学习API
以sap leonardo作为关键字在微信上搜索,能搜到不少文章.但是我浏览了一下,好像没有发现有从具体编程角度上来介绍的.所以我就贡献一篇. 需求 开发一个Java程序,用户可以指定一张图片,该Ja ...
- 【UVA10652】Board Wrapping(求凸包面积)
点此看题面 大致题意: 告诉你若干个矩形的重心坐标.长.宽和相对\(y\)轴的偏转角度,求矩形面积和与能围住这些矩形的最小凸包面积之比. 矩形面积和 这应该是比较好求的吧. 已经给了你长和宽,直接乘起 ...
- 【洛谷2577】[ZJOI2005] 午餐(较水DP)
点此看题面 大致题意: 有\(N\)个学生去食堂打饭,每个学生有两个属性:打饭时间\(a_i\)和吃饭时间\(b_i\).现要求将这些学生分成两队分别打饭,求最早何时所有人吃完饭. 贪心 首先,依据贪 ...
- 浅谈 import / export
import { ngModule } from '@angular/core'; import { AppComponent } from './app.component'; export cla ...
- WARNING: The TCP backlog setting of 511.解决
redis启动警告问题:WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/so ...