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 ...
随机推荐
- java数据结构和算法07(2-3-4树)
上一篇我们大概了解了红黑树到底是个什么鬼,这篇我们可以看看另外一种树-----2-3-4树,看这个树的名字就觉得很奇怪.... 我们首先要知道这里的2.3.4指的是任意一个节点拥有的子节点个数,所以我 ...
- 记录下laravel 5.2的auth/logout路由工作不正常的问题
- Erlang 001--开篇
有段时间没有更新博客了,最近稍微接触了下一门相对小众的语言Erlang,个人感觉学习一段时间有必要总结总结,本文作为该系列的开篇,仅仅列举一些与Java的一些不同点和个人对Erlang的一些主观印象, ...
- shiro web环境初始化过程
在web工程中使用shiro的时候需要配置一个shiro的listenser(EnvironmentLoaderListener)和一个shiro的filter(ShiroFilter). liste ...
- PowerShell (407) Proxy Authentication Required
$Client = New-Object -TypeName System.Net.WebClient $Client.Proxy.Credentials = [System.Net.Credenti ...
- 爬虫基础-http请求的基础知识
百度百科上这么介绍爬虫: 网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本. 在开发爬虫时常用的工具:ch ...
- git图形管理工具
在windows下使用git命令行工具对非开发人员还是挺困难的,还好有TortoiseGit这个工具svn客户端用TortoiseSVNgit客户端用TortoiseGit 网址:https://to ...
- 【洛谷1501】[国家集训队] Tree II(LCT维护懒惰标记)
点此看题面 大致题意: 有一棵初始边权全为\(1\)的树,四种操作:将两点间路径边权都加上一个数,删一条边.加一条新边,将两点间路径边权都加上一个数,询问两点间路径权值和. 序列版 这道题有一个序列版 ...
- NBU基本常用命令
Veritas常用命令: 1. 查看当有运行的任务 bpdbjobs –report | grep Active 2. 停止任务 bpdbjobs –cancel PID (包括主任务和子任务) 3. ...
- fflush - 刷新一个流
SYNOPSIS 总览 #include <stdio.h> int fflush(FILE *stream); DESCRIPTION 描述 函数 fflush 强制在所给的输出流或更新 ...