Codeforces 830A. Office Keys (贪心二分 or DP)
原题链接: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)的更多相关文章
- CF830A Office Keys(贪心)
CF830A Office Keys [题目链接]CF830A Office Keys [题目类型]贪心 &题意: 有n个人,k个钥匙,一个目的地,求让n个人都回到目的地的最短时间,每个人都要 ...
- bzoj 2067: [Poi2004]SZN【贪心+二分+树形dp】
第一问就是Σ(deg[u]-1)/2+1 第二问是二分,判断的时候考虑第一问的贪心规则,对于奇度数的点,两两配对之后一条延伸到上面:对于欧度数的点,两两配对或者deg[u]-2的点配对,然后一条断在这 ...
- Codeforces 865C Gotta Go Fast 二分 + 期望dp (看题解)
第一次看到这种骚东西, 期望还能二分的啊??? 因为存在重置的操作, 所以我们再dp的过程中有环存在. 为了消除环的影响, 我们二分dp[ 0 ][ 0 ]的值, 与通过dp得出的dp[ 0 ][ 0 ...
- Codeforces 825D Suitable Replacement - 贪心 - 二分答案
You are given two strings s and t consisting of small Latin letters, string s can also contain '?' c ...
- AC日记——830A - Office Keys
思路: 背包: 代码: #include <cmath> #include <cstdio> #include <cstring> #include <ios ...
- BZOJ 1046: [HAOI2007]上升序列【贪心+二分状态+dp+递归】
1046: [HAOI2007]上升序列 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4987 Solved: 1732[Submit][Stat ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Powershell指令集_2
目录 目录 获取证书 Get-Childitem 调用REST API Invoke-RestMethod 选择对象属性 Select-Object 导入模块 Invoke-Expression 路径 ...
- 测开之路一百零八:bootstrap表格
引入bootstrap和jquery 普通表格 html自带的边框线 bootstrap表格属性 bootstrap表格 边框线 鼠标经过变色 压缩表格,减小密度 自适应屏幕 隔行突出(变色) 表格里 ...
- 35 怎么优化join
35 怎么优化join 上一篇介绍了join的两种算法:nlj和bnl create table t1(id int primary key, a int, b int, index(a)); cre ...
- C#实体类生成Create Table SQL
using System; using System.Collections.Generic; using System.Text; using System.Reflection; namespac ...
- ES6新增特性
ES6: ECMA 第六次改版 块级作用域: 凡是被{ }包裹住的代码都是块级作用域,除了对象 特点:会造成一个暂时性死区 新增声明变量的两种方式: let: a. ...
- Java包的使用
好处 1.类似于文件系统的文件夹,可以实现分类管理类文件,方便查找2.解决了同一个项目中同名类的冲突问题 包的创建 命名规范: 建议小写字母,并且采用域名倒置的写法 域名倒置:com.baidu ww ...
- JAVA10以上版本 搜索不到 dt.jar和tools.jar
从jdk-9之后就已经没有tools.jar和dt.jar了,也不需要在classpath里面配置这些jar了,配置可参考:JAVA_HOME=jdk安装路径JRE_HOME=jre安装路径PATH= ...
- Spring事务管理之几种方式实现事务(转)
一:事务认识 大家所了解的事务Transaction,它是一些列严密操作动作,要么都操作完成,要么都回滚撤销.Spring事务管理基于底层数据库本身的事务处理机制.数据库事务的基础,是掌握Spring ...
- selenium自动新增店铺
说明:仅作为参考练习,代码中涉及数据均为测试数据. from selenium import webdriver from selenium.webdriver import ActionChains ...
- MIT 6.824学习笔记4 Lab1
现在我们准备做第一个作业Lab1啦 wjk大神也在做6.824,可以参考大神的笔记https://github.com/zzzyyyxxxmmm/MIT6824_Distribute_System P ...