Codeforces 831D Office Keys【Dp/二分】
来源:互联网 发布:最优化问题的典型案例 编辑:程序博客网 时间:2024/05/13 19:27
There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.
You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
The first line contains three integers n,k andp (1 ≤ n ≤ 1 000,n ≤ k ≤ 2 000,1 ≤ p ≤ 109) — the number of people, the number of keys and the office location.
The second line contains n distinct integersa1, a2, ..., an (1 ≤ ai ≤ 109) — positions in which people are located initially. The positions are given in arbitrary order.
The third line contains k distinct integersb1, b2, ..., bk (1 ≤ bj ≤ 109) — positions of the keys. The positions are given in arbitrary order.
Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
Print the minimum time (in seconds) needed for all n to reach the office with keys.
2 4 5020 10060 10 40 80
50
1 2 101115 7
7
In the first example the person located at point 20 should take the key located at point40 and go with it to the office located at point50. He spends30 seconds. The person located at point100 can take the key located at point80 and go to the office with it. He spends50 seconds. Thus, after50 seconds everybody is in office with keys.
题目大意:
给你N个人,有K把钥匙,终点为P.
每个人要求拿一把钥匙到终点,问多长时间,能够使得所有人都做到。
思路:
设定Dp【i】【j】表示前i个人,在前j把钥匙中都拿到了钥匙,并且到终点的最优最长需求时间。
那么有:
if(i==j)Dp【i】【j】=max(Dp【i-1】【j-1】,第i个人走到钥匙j并且到终点的花费);
else Dp【i】【j】=min(Dp【i】【j-1】,max(Dp【i-1】【j-1】,第i个人走到钥匙j并且到终点的花费));
Ac代码:
#include<stdio.h>#include<string.h>#include<algorithm>#include<climits>using namespace std;int a[150000];int b[150000];int dp[1005][1005];int main(){ int n,k,p; while(~scanf("%d%d%d",&n,&k,&p)) { memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++)scanf("%d",&a[i]); for(int i=1;i<=k;i++)scanf("%d",&b[i]); sort(a+1,a+1+n); sort(b+1,b+1+k); for(int i=1;i<=n;i++) { for(int j=i;j<=k;j++) { if(i==j) { dp[i][j]=max(dp[i-1][j-1],abs(p-b[j])+abs(a[i]-b[j])); continue; } dp[i][j]=min(dp[i][j-1],max(dp[i-1][j-1],abs(p-b[j])+abs(a[i]-b[j]))); } } int output=INT_MAX; for(int i=n;i<=k;i++) { output=min(output,dp[n][i]); } printf("%d\n",output); }}
二分思路:
二分时间,贪心check.
每个人,尽量拿左边的钥匙即可。
Ac代码:
#include<stdio.h>#include<string.h>#include<algorithm>#include<climits>using namespace std;#define ll __int64ll a[150000];ll b[150000];ll n,k,p;ll Slove(ll mid){ ll output=0; ll now=1; for(ll i=1;i<=n;i++) { while(now<=k&&abs(b[now]-a[i])+abs(b[now]-p)>mid) { now++; } if(now<=k) { now++; output++; } else break; } if(output>=n)return 1; else return 0;}int main(){ while(~scanf("%I64d%I64d%I64d",&n,&k,&p)) { for(ll i=1;i<=n;i++)scanf("%I64d",&a[i]); for(ll i=1;i<=k;i++)scanf("%I64d",&b[i]); sort(a+1,a+1+n); sort(b+1,b+1+k); ll ans=-1; ll l=0; ll r=2000000000+50; while(r>=l) { ll mid=(l+r)/2; if(Slove(mid)==1) { ans=mid; r=mid-1; } else l=mid+1; } printf("%I64d\n",ans); }}
- Codeforces 831D Office Keys【Dp/二分】
- Codeforces 831D Office Keys 二分+贪心
- Codeforces 831D Office Keys【二分 】
- codeforces 831 D Office Keys(dp)
- Codeforces Round #424 -(二分&贪心)||dp-D. Office Keys
- Codeforces 831D Office Keys(思维or二分)
- CodeForces 831D Office Keys
- Codeforces 831 D Office Keys
- codeforces 831D Office Keys
- Codeforces 831D Office Keys 贪心or dp
- Codeforces Round #424 (Div. 2) D. Office Keys(贪心 二分 or DP)
- Codeforces #831D: Office Keys 题解
- Codeforces Round #424 D. Office Keys(二分)
- Codeforces Round #424 (Div. 2) D. Office Keys(dp)
- 【贪心+二分查找】Office Keys CodeForces
- Codeforces 830A. Office Keys 【dp】
- 【dp】codeforces 830-A Office Keys
- CF Round #424( Div.2) D. Office Keys 【二分||DP】
- KEIL 代码文件中添加标签
- Lintcode刷题Day01
- Linux网络配置(虚拟机版)
- Python-VirtualEnv的使用
- 编码方式
- Codeforces 831D Office Keys【Dp/二分】
- brackets打开大项目的解决办法
- TCP的4个定时器
- 分布式架构 -- 学习路线图
- 怎么让Chrome支持小于12px 的文字?
- win10系统下jdk环境变量的不同之处,避免javac不是内部或外部命令
- Java NIO 详解(一)
- POS机一般分为5大类
- 拒做伸手党:自己动手找资源