Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) - D
题目链接:http://codeforces.com/contest/831/problem/D
题意:在一个一维坐标里,有n个人,k把钥匙(钥匙出现的位置不会重复并且对应位置只有一把钥匙),和一个终点p。问你每个人都拿到一把钥匙并且回到终点的情况下,n个人之中所花时间最长的那个人时间最少是多少?(一秒只能走一个单位的距离)
思路:考虑二分x,x为每个人能走的步数。对于两个人a,b和两把钥匙c,d 那么当p[a]<p[b]并且p[c]<p[d]时, a拿c钥匙,b拿d钥匙是最优的,因为对于p[c]到终点p的距离和p[d]到终点p的距离是固定的,但是如果a拿d, b拿c的话则出现交叉距离会更大,所花时间更大(贪心); 所以我们对于人和钥匙排个序,然后对于每个人都去拿在步数小于x的情况下(x包括从起点到拿钥匙,在从钥匙位置到终点的距离),最左边的那把钥匙。 然后预处理一下每个人拿每一个钥匙并且回到终点的时间即可。
import java.io.*;
import java.util.*; public class Main {
public static final int MAXN=1000+24;
public static final int MAXK=2000+24;
public static final int INF=((int)2e9)+24;
public static int n,k,p;
public static int[] pos=new int[MAXN];
public static int[] keys=new int[MAXK];
public static boolean[] vis=new boolean[MAXK];
public static int[][] dist=new int[MAXN][MAXK]; public static boolean check(int x){
Arrays.fill(vis, false);
for(int i=0;i<n;i++){
int keypos=-1;
for(int j=0;j<k;j++){
if(dist[i][j]<=x&&vis[j]==false){
keypos=j; break;
}
}
if(keypos==-1){
return false;
}
vis[keypos]=true;
}
return true;
} public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
PrintWriter out=new PrintWriter(System.out);
n=cin.nextInt(); k=cin.nextInt(); p=cin.nextInt();
for(int i=0;i<n;i++){
pos[i]=cin.nextInt();
}
for(int i=0;i<k;i++){
keys[i]=cin.nextInt();
}
Arrays.sort(pos,0,n);
Arrays.sort(keys,0,k);
for(int i=0;i<n;i++){
Arrays.fill(dist[i],0);
}
for(int i=0;i<n;i++){
for(int j=0;j<k;j++){
dist[i][j]=Math.abs(pos[i]-keys[j])+Math.abs(keys[j]-p);
// out.printf("%d ",dist[i][j]);
}
// out.println();
}
int l=0,r=INF,mid;
while(r>=l){
mid=l + ((r - l) >> 1);
if(check(mid)){
r=mid-1;
}else{
l=mid+1;
}
}
out.println(l);
out.flush(); out.close(); cin.close();
}
}
Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) - D的更多相关文章
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)
http://codeforces.com/contest/831 A. Unimodal Array time limit per test 1 second memory limit per te ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem C (Codeforces 831C) - 暴力 - 二分法
Polycarp watched TV-show where k jury members one by one rated a participant by adding him a certain ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)A,B,C
A:链接:http://codeforces.com/contest/831/problem/A 解题思路: 从前往后分别统计递增,相等,递减序列的长度,如果最后长度和原序列长度相等那么就输出yes: ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem F (Codeforces 831F) - 数论 - 暴力
题目传送门 传送门I 传送门II 传送门III 题目大意 求一个满足$d\sum_{i = 1}^{n} \left \lceil \frac{a_i}{d} \right \rceil - \sum ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
There are n people and k keys on a straight line. Every person wants to get to the office which is l ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 831E) - 线段树 - 树状数组
Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this int ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem A - B
Array of integers is unimodal, if: it is strictly increasing in the beginning; after that it is cons ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) A 水 B stl C stl D 暴力 E 树状数组
A. Unimodal Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) - C
题目链接:http://codeforces.com/contest/831/problem/C 题意:给定k个评委,n个中间结果. 假设参赛者初始分数为x,按顺序累加这k个评委的给分后得到k个结果, ...
随机推荐
- <知识整理>2019清北学堂提高储备D2
简单数据结构: 一.二叉搜索树 1.前置技能: n/1+n/2+……+n/n=O(n log n) (本天复杂度常涉及) 2.入门题引入: N<=100000. 这里多了一个删除的操作,因此要 ...
- [BZOJ3779]重组病毒:Link-Cut Tree+线段树
分析 其实其他的题解说的都很清楚了. 一个点出发感染到根结点所花费的时间是路径上虚边的条数+1. RELEASE相当于\(access()\). RECENTER相当于\(makeroot()\).( ...
- [CSP-S模拟测试]:糊涂图(概率DP)
题目传送门(内部题76) 输入格式 第一行输入三个空格隔开的整数$n,m,s$表示随机加一条边之前的糊涂图的点数,边数,以及起点的编号. 接下来$m$行,每行两个空格隔开的整数$a,b$表示从$a$到 ...
- (实战)多边形,梯形盒阴影css实现技巧
一般情况下,我们给块状元素(四边形)添加阴影样式,直接用css box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);就可以了,但是总有一些需求是那么的特别,例如下图: ...
- 杂项-站点:SharePoint
ylbtech-杂项-门户站点:SharePoint SharePoint Portal Server 2003 是一个门户站点,使得企业能够开发出智能的门户站点,这个站点能够无缝连接到用户.团队和知 ...
- python -加密(MD5)
import hashlib def md5_passwd(str,salt ='aaaaa') str = str + salt m = hashlib.md5()#构造一个MD5对象 m.upda ...
- 阶段1 语言基础+高级_1-3-Java语言高级_07-网络编程_第4节 模拟BS服务器案例_2_模拟BS服务器代码实现
这三行代码是固定的在输出之前 浏览器再次访问这个页面. 图片没有显示出来 复制刚才的代码一份出来重命名 加个while循环.把代码都放进去. 然后在while里面开启一个线程.把读取的代码都放在线程里 ...
- shell脚本一一项目5
主题:一键查看占用内存.cpu高的进程 echo "----------------cpu top 10 list----------------"ps -eo pid,pcpu, ...
- C# datatable 导出到Excel
datatable导出到Excel /// <summary> /// 将DataTable导出为Excel文件(.xls) /// </summary> /// <pa ...
- 【SD系列】SAP SD模块-公司间销售简介
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[SD系列]SAP SD模块-公司间销售简介 ...