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个结果, ...
随机推荐
- 阿里云万网虚拟主机安装配置Https(SSL)教程
太多太多的用户咨询阿里云虚拟主机是否可以安装SSL数字证书?万网空间是否可以支持HTTPS协议访问网站?答案只有一个:目前阿里云虚拟主机都不支持安装SSL证书!但是,但是,可以曲线实现目标! 1.为了 ...
- wap开发tips
1.overflow-x 这真的是一个大坑,一旦你在body或者html上用了这个属性,对不起,如果你的页面出现滚动条的话,那就会出现莫名其妙的bug,滑动页面的时候fix在顶部或者底部的会挡住. 解 ...
- Oracle诊断:在程序的运行中,有时候数据库会断开连接
在程序的运行中,有时候数据库会断开连接,然后报下面错误: ORA-12519: TNS:no appropriate service handler found 可用的服务处理程序没有找到. 1. ...
- React-Native 之 GD (五)属性声明和属性确认 及 占位图
1.在 React-Native 创建的自定义组件是可以复用的,而开发过程中一个组件可能会由多个人同时开发或者多个人使用一个组件,为了让开发人员之间减少沟通成本,我们会对某些必要的属性进行属性声明,让 ...
- ARM非对齐访问和Alignment Fault
1.指令对齐 A64指令必须word对齐.尝试在非对齐地址取值会触发PC alignment fault. 1.1.PC alignment checking PC(Program Counter)寄 ...
- Python编程:从入门到实践—函数
从函数中修改列表 一家为用户提交的设计制作3D打印模型的公司,需要打印的设计存储在一个列表中,打印后移到另一个列表中. #!/usr/bin/env python # -*- coding:utf-8 ...
- dp培训完结(8.9)
概率与期望dp 期望: 为什么下面的式子成立? 若x可以取1,2,3,则x+c可以取1+c,2+c,3+c..........x*c可以取1*c,2*c,3*c why? 举个例子(E(x+y)=E( ...
- C# 防火墙操作之创建规则
对于某些程序,我们只允许它使用某些特定端口.网络类型或者特定IP类型等信息.这时候,需要使用到防火墙里面的“高级设置”,创建某些特定的入站或者出栈规则,以规避其程序使用允许端口等意外的信息. 下面以创 ...
- 【转】最全的 pip 使用指南,50% 你可能没用过
[转]最全的 pip 使用指南,50% 你可能没用过 所有的 Python 开发者都清楚,Python 之所以如此受欢迎,能够在众多高级语言中,脱颖而出,除了语法简单,上手容易之外,更多还要归功于 P ...
- GenericAPIView的使用及和视图扩展类的结合使用
GenericAPIView的使用 from rest_framework.generics import GenericAPIView GenericAPIView继承 APIView,主要增加了操 ...