题目链接: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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)A,B,C

    A:链接:http://codeforces.com/contest/831/problem/A 解题思路: 从前往后分别统计递增,相等,递减序列的长度,如果最后长度和原序列长度相等那么就输出yes: ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) - C

    题目链接:http://codeforces.com/contest/831/problem/C 题意:给定k个评委,n个中间结果. 假设参赛者初始分数为x,按顺序累加这k个评委的给分后得到k个结果, ...

随机推荐

  1. [CSP-S模拟测试]:超级树(DP)

    题目传送门(内部题5) 输入格式 一行两个整数$k$.$mod$,意义见上. 输出格式 一行一个整数,代表答案. 样例 样例输入1: 2 100 样例输出1: 样例输入2: 3 1000 样例输出2: ...

  2. UVA 1380 A Scheduling Problem

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  3. The import org.apache.hadoop.mapreduce cannot be resolved

    ubuntu@VM---ubuntu:~$ sudo apt--src.tar.gz Reading package lists... Done Building dependency tree Re ...

  4. 豆瓣镜像安装python库

  5. Android的Surface的创建

    ViewRootImpl管理着整个view tree. 对于ViewRootImpl.setView(),我们可以简单的把它当做一个UI渲染操作的入口. http://androidxref.com/ ...

  6. PHP 实现并发-进程控制 PCNTL

    参考 基于PCNTL的PHP并发编程 PCNTL 是 PHP 中的一组进程控制函数,可以用来 fork(创建)进程,传输控制信号等. 在PHP中,进程控制支持默认关闭.编译时通过 --enable-p ...

  7. js-url操作记录

    禁用回退&开启回退 // 必须声明方法 否则无法删除此监听器 function backCommon() { history.pushState(null, null, document.UR ...

  8. mysql数据库连接 application.properties

    # 数据库链接信息mysql.driver=com.mysql.cj.jdbc.Drivermysql.url=jdbc:mysql://localhost:3306/xxxx?characterEn ...

  9. hbase的TTL机制清除opentsdb的超时数据

    我们发现用opentsdb向hbase写数据之后,磁盘占用率飙升得很快,我们存的业务数据只用保存一个月的即可,了解hbase的TTL机制可以清除相关表.相关行的超时数据,之前在数据备份时,我介绍了,o ...

  10. virutalenv一次行安装多个requirements里的文件