题目链接: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. 洛谷P1982 小朋友的数字——题解

    题目传送 简单地说,这题就是让我们求前i个数的最大子串和和最值. 对于最大子串和,我们可以设一个变量qian,表示以当前元素结尾的最大子串的子串和.若搜索完第i-1个小朋友,现在看到第i个小朋友时,若 ...

  2. bat语法

    注释 :: 注释无回显 rem 注释有回显 关闭和开启回显 :: 关闭回显 @echo off echo abc :: 开启回显 echo on echo 查看命令帮助说明 rd /? 目录操作 创建 ...

  3. HDU6599 (字符串哈希+回文自动机)

    题意: 求有多少个回文串的前⌈len/2⌉个字符也是回文串.(两组解可重复)将这些回文串按长度分类,分别输出长度为1,2,...,n的合法串的数量. 题解:https://www.cnblogs.co ...

  4. 关于 Visual stdio 编译报错:error MSB6006: “CL.exe”已退出

    网上查看,原因有多种. 1,我自己遇到的是这样的: 环境:VS2019,编译项目 image-master,中间自己重整了原来的目录,移动了很多文件.编译报错:error MSB6006: “CL.e ...

  5. 【HTML】<!DOCTYPE html>作用

    1.定义: DOCTYPE标签是一种标准通用标记语言的文档类型声明,它的目的是要告诉标准通用标记语言解析器,它应该使用什么样的文档类型定义(DTD)来解析文档. <!DOCTYPE> 声明 ...

  6. spring boot 整合 RabbitMQ 错误

    1.错误 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.spr ...

  7. c++调用c#代码

    // ConsoleApplication1.cpp : 此文件包含 "main" 函数.程序执行将在此处开始并结束. // #include "pch.h" ...

  8. js-用判断音乐或图片是否加载完成的方式来控制页面的现实

    判断页面加载,加完完成后,内容页显示,加载条隐藏 百度搜索方法很多,大多都是: document.onreadystatechange = function() //当页面加载状态改变的时候执行fun ...

  9. 【Python】Visual Studio Code 安装&&使用 hello python~~~~

    1.安装Python 官网下载: https://www.python.org/downloads/   选择版本下载 2.下载完毕后,点击安装. 3.看到页面,直接下一步,全部默认选项. 4.安装即 ...

  10. charles_01_打断点修改接口请求&返回数据

    前言 测试过程中,为了模拟某场景测试,经常需要修改接口请求或者返回数据.可通过抓包工具打断点,实现模拟测试场景.常用的抓包工具charles和fiddler均可打断点mock数据.由于小编安装了cha ...