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 ...
随机推荐
- LVS Direct Routing 直接路由
1. Direct Routing 直接路由 director分配请求到不同的real server, real server处理请求后直接回应给用户,这样director负载均衡器仅处理客户机与服务 ...
- oracle报错:ORA-01658(转自52斋347)
在oracle里创建表,报出错:ORA-01658: 无法为表空间space中的段创建 INITIAL 区:或者: ORA-01658: unable to create INITIAL extent ...
- php 出现空格,换行原因
1.空格:转换成utf-8无bom格式 2.空格:<?php ?>标签结尾有中文,英文空格 3.换行,一个文件中有多个<?php ?>标签,标签间隔中有换行符合
- 转载 tomcat6下项目移植到tomcat7下出问题的解决办法
转载,原文地址 http://hw1287789687.iteye.com/blog/1817865 org.apache.catalina.core.ContainerBase addChildI ...
- LAMP Stack 5.7.16 (Ubuntu 16.04.1)
平台: Ubuntu 类型: 虚拟机镜像 软件包: apache2.4 mysql5.7 php7 phpmyadmin4.5 apache application server basic soft ...
- 索引是否也能提高UPDATE,DELETE,INSERT速度 解释
insert 不会提高,insert 用不到索引,只会增加维护索引的时间. update ,更新索引列不会提高,少量更新非索引列,会有提高 : 更新索引列,索引要重新维护,更新非索引列,倒是没什么影响 ...
- ubuntu openstack windows 镜像制作
openstack windows 镜像制作 首先下载windows所需要的驱动,virtio-win-1.1.16.vfd virtio-win-0.1-59.iso 下载的官方地址是: http ...
- PHP:implode(),emplode() 字符串数组,数组字符串转换函数
1.implode()-Join array elements with a string(把数组元素组合为一个字符串.) string implode([string $separator,] ar ...
- IOS Window窗口使用
// 程序启动完毕之后就会调用一次 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NS ...
- Uploadify 3.2 参数属性、事件、方法函数详解以及配置
一.属性 属性名称 默认值 说明 auto true 设置为true当选择文件后就直接上传了,为false需要点击上传按钮才上传 . buttonClass ” 按钮样式 buttonCursor ‘ ...