原题链接: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. bp文件错误消除

    代码文件报错, error: unused parameter 'data' [-Werror,-Wunused-parameter]‘ 按提示在cflags中加入: "-Wunused-p ...

  2. 嗯,python

    总觉得在这么个地方已经没有在碰blog的可能了...但是... 突然说要用python来配置环境...好歹也是这个专业的啊...还是 看看吧... 然后 百度一搜,看到一个 好的 网站,不知道 我一旦 ...

  3. tips for using shortcuts

    tips for using shortcuts for mac: command+ctrl+F:full screen(当前应用全屏之后有一个好处 就是 使用 4 tap 的手势 可以在全屏的界面之 ...

  4. idea 社区版本创建javaweb项目 使用tomcat

    1.创建maven  webapp项目 2.pom文件添加依赖及tomcat7-maven-plugin插件 <dependencies> <dependency> <g ...

  5. Django中ORM的聚合索引

    Django中ORM的聚合索引   在Django中,聚合函数是通过aggregate方法实现的,aggregate方法返回的结果是一个字典 在使用时需要先导入模块from django.db.mod ...

  6. 初学css list-style属性

    网上很多css布局中会看到这样的一句:list-style:none: 那么list-style到底什么意思?中文即:列表样式:无: 其实它是一个简写属性,包含了所有列表属性,具体包含list-sty ...

  7. MIT 6.824学习笔记4 Lab1

    现在我们准备做第一个作业Lab1啦 wjk大神也在做6.824,可以参考大神的笔记https://github.com/zzzyyyxxxmmm/MIT6824_Distribute_System P ...

  8. asp.net core2.1认证和授权解密

    来源:https://www.cnblogs.com/pangjianxin/p/9372562.html asp.net core2.1认证和授权解密 本篇文章翻译自:https://digital ...

  9. SQL Server 查找字符串中指定字符出现的次数

    要查找某个指定的字符在字符串中出现的位置,方法比较简单,使用 len() 函数和 replace() 函数结合就可以. SELECT TOP 200 approveInfo approveInfo2, ...

  10. MySQL concat函数里面单引号的使用

    通过concat拼字符串的时候,如果语句里面需要使用单引号,可以使用两个单引号来代替一个引号