原题链接:http://codeforces.com/contest/830/problem/A

题意:在一条数轴上分别有n个人和k把钥匙(n<=k),以及一个目的地,每个人要各自拿到一个钥匙后到达目的地。每个人的移动速度都是1, 问所有人都到达目的地的最短时间。

思路:转化一下题意,就是求耗时最长的人所用的最短时间。

我们可以二分答案x,然后对排序后的人以及钥匙进行枚举,进行从左至右搭配。 这里check函数中返回false的条件是从左至右所有人都能在x的时间内到达目的地,而计算这些人到达目的地的时间是:取最近的一对人与钥匙计算,为了每个人耗时最少(贪心),这是最优的。

AC代码:

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int MAXN=;
int n,k;
int a[MAXN],b[MAXN],S;
bool check(LL x){
int p=;
for(int i=;i<=n;i++){
while(p<=k){
p++;
if(1LL*(abs(a[i]-b[p])+abs(b[p]-S))<=x) break;
}
if(p>k) return false;
}
return true;
}
int main()
{
scanf("%d %d %d", &n , &k, &S);
for(int i=;i<=n;i++) scanf("%d", &a[i]);
for(int i=;i<=k;i++) scanf("%d", &b[i]);
sort(a+, a+n+);
sort(b+, b+k+);
LL l=,r=2e9+;
LL mid;
while(l<r){
mid=(l+r)/;
if(check(1LL*mid)) r=mid;
else l=mid+;
}
printf("%d\n", l);
}

这道题也可以用dp来做: dp[i][j]表示前i个人有前j个钥匙的最优解,利用01背包思想可以在O(n*k)时间内求出答案。

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int INF=2e9+;
const int MAXN=;
int n,k;
int a[MAXN],b[MAXN],S;
int dp[MAXN][MAXN];
int main(){
scanf("%d %d %d", &n , &k, &S);
for(int i=;i<=n;i++) scanf("%d", &a[i]);
for(int i=;i<=k;i++) scanf("%d", &b[i]);
sort(a+,a++n);
sort(b+,b++k);
for(int i=; i<=n; i++)
for(int j=; j<=k; j++)
dp[i][j] = INF;
for(int i=; i<=k; i++) dp[][i]=;
for(int i=; i<=n; i++)
for(int j=i; j<=k; j++){
dp[i][j] = min(dp[i][j-],max(dp[i-][j-],abs(a[i]-b[j])+abs(b[j]-S)));
}
printf("%d\n", dp[n][k]);
return ;
}

Codeforces 830A. Office Keys (贪心二分 or DP)的更多相关文章

  1. CF830A Office Keys(贪心)

    CF830A Office Keys [题目链接]CF830A Office Keys [题目类型]贪心 &题意: 有n个人,k个钥匙,一个目的地,求让n个人都回到目的地的最短时间,每个人都要 ...

  2. bzoj 2067: [Poi2004]SZN【贪心+二分+树形dp】

    第一问就是Σ(deg[u]-1)/2+1 第二问是二分,判断的时候考虑第一问的贪心规则,对于奇度数的点,两两配对之后一条延伸到上面:对于欧度数的点,两两配对或者deg[u]-2的点配对,然后一条断在这 ...

  3. Codeforces 865C Gotta Go Fast 二分 + 期望dp (看题解)

    第一次看到这种骚东西, 期望还能二分的啊??? 因为存在重置的操作, 所以我们再dp的过程中有环存在. 为了消除环的影响, 我们二分dp[ 0 ][ 0 ]的值, 与通过dp得出的dp[ 0 ][ 0 ...

  4. Codeforces 825D Suitable Replacement - 贪心 - 二分答案

    You are given two strings s and t consisting of small Latin letters, string s can also contain '?' c ...

  5. AC日记——830A - Office Keys

    思路: 背包: 代码: #include <cmath> #include <cstdio> #include <cstring> #include <ios ...

  6. BZOJ 1046: [HAOI2007]上升序列【贪心+二分状态+dp+递归】

    1046: [HAOI2007]上升序列 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4987  Solved: 1732[Submit][Stat ...

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

  8. Codeforces Round #768 (Div. 2) D. Range and Partition // 思维 + 贪心 + 二分查找

    The link to problem:Problem - D - Codeforces   D. Range and Partition  time limit per test: 2 second ...

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

随机推荐

  1. 20170809--JS操作Select备忘

    // 1.判断select选项中 是否存在Value="paraValue"的Item function jsSelectIsExitItem(objSelect, objItem ...

  2. 阶段1 语言基础+高级_1-3-Java语言高级_05-异常与多线程_第6节 Lambda表达式_1_函数式编程思想概述

    函数式编程和面向对象的区别

  3. Delphi控件-复合控件

     http://blog.csdn.net/cml2030/article/details/3166634 Delphi控件-复合控件 标签: delphidestructorbuttonstring ...

  4. Java ——Number & Math 类 装箱 拆箱 代码块

    本节重点思维导图 当需要使用数字的时候,我们通常使用内置数据类型,如:byte.int.long.double 等 int a = 5000; float b = 13.65f; byte c = 0 ...

  5. 【ABAP系列】SAP abap dialog screen屏幕参数简介

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP abap dialog ...

  6. SpringCloud启动Eureka server时报错 java.lang.TypeNotPresentException: Type javax.xml.bind.JAXBContext not present

    SpringBoot打开Eureka server时出现以下错误: java.lang.TypeNotPresentException: Type javax.xml.bind.JAXBContext ...

  7. Monte Carlo Control

    Problem of State-Value Function Similar as Policy Iteration in Model-Based Learning, Generalized Pol ...

  8. (转)http://blog.chinaunix.net/uid-8363656-id-2031644.html CGI 编写

    第一章:基础的基础 回CGI教程目录 1.1 为什么使用CGI?   我没有把什么是CGI放在基础篇的第一段,是因为实在很难说明白到底什么是CGI.而如果你先知道CGI有什么作用,将会很好的理解CGI ...

  9. 简述Vue中的过滤器

    1.过滤器的基本概念 概念:本质上是函数: 作用:用户输入数据后,它能够进行处理,并返回一个数据结果:(无return语句不会报错,但是这种过滤器没有丝毫意义) 格式:管道符(  |  )进行连接,而 ...

  10. python基础-10 程序目录结构 学生选课系统面向对象练习

    一 程序目录结构 1 bin文件夹 二进制文件.代码程序  2 conf 配置文件  3 帮助文档  4 头文件库文件等 二 学生选课系统部分代码 未完待续 1 包内的__init__.py文件 在包 ...