Meaning
n individuals going to a place p and give everyone a one-dimensional coordinate a[i], go to this place need a key, a total of K key, given the coordinates of each key b[i], ask N people finally arrived at this place how much time, each person to walk a unit distance need a unit time.
Ideas for solving problems:
Looks very complicated question, has the destination coordinates and the key coordinates, actually thinks carefully, after takes the key, then also needs the time also to decide, therefore only needs to consider the person and the key relations can.
DP[I][J] can be defined as the maximum time for the former I person to take I to the destination in the former J key.
The transfer is dp[i][j]=min (Dp[i][j-1], Max (Dp[i-1][j-1], ABS (a[i]-b[i) +abs (b[i]-p));
Don't forget to sort, don't forget long long and you're done.
Code:
#include <bits/stdc++.h>
#define LL long-long
using namespace std;
Const LL INF=1E16;
LL dp[1005][2005];
LL a[1005];
LL b[2006];
LL p;
int main ()
{
int n, I, J, K;
cin>>n>>k>>p;
For (I=1 i<=n; i++) scanf ("%lld", &a[i));
Sort (a+1, a+n+1);
For (I=1 i<=k; i++) scanf ("%lld", &b[i));
Sort (b+1, b+k+1);
for (j=0; j<=k; j + +) dp[0][j]=0;
For (I=1 i<=n; i++)
{for
(j=i; j<=k; j + +)
{
if (i==j)
{
Dp[i][j]=max (dp[i-1][ J-1],abs (A[i]-b[j]) +abs (b[j]-p));
Continue;
}
Dp[i][j]=min (Dp[i][j-1], Max (Dp[i-1][j-1],abs (a[i]-b[j)) +abs (b[j]-p));
}
J=n;
For (i=n i<=k; i++)
{
if (dp[n][i]<dp[n][j)) j=i; cout<<dp[n][i]<<endl;
}
cout<<dp[n][j]<<endl;
}