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 50 20 100 60 10 40 80
50
1 2 10 11 15 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 spends 30 seconds. The person located at point100 can take the key located at point80 and go to the office with it. He spends 50 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 __int64
ll 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);
}
}