A. Office Keys (from Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals) )

时间:2022-12-30 10:58:13
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 int a[150000];
 8 int b[150000];
 9 int dp[1005][1005];
10 //dp[i][j] 前i个人从前j个药匙中到达终点的最小时间
11 
12 int main()
13 {
14     int n,k,p;
15     while(~scanf("%d%d%d",&n,&k,&p))
16     {
17         memset(dp,0,sizeof(dp));
18         for(int i=1;i<=n;i++) scanf("%d",&a[i]);
19         for(int i=1;i<=k;i++) scanf("%d",&b[i]);
20         sort(a+1,a+1+n);
21         sort(b+1,b+1+k);
22 
23         for(int i=1;i<=n;i++)
24         {
25             for(int j=i;j<=k;j++)
26             {
27                 if(i==j)
28                 {
29                     dp[i][j]=max(dp[i-1][j-1],abs(p-b[j])+abs(a[i]-b[j]));
30                     continue;
31                 }
32                 dp[i][j]=min(dp[i][j-1],max(dp[i-1][j-1],abs(p-b[j])+abs(a[i]-b[j])));
33             }
34         }
35 
36         int ans = INT_MAX;
37         for(int i = n;i <= k;i++)
38         {
39             ans = min(ans,dp[n][i]);
40         }
41         printf("%d\n",ans);
42     }
43     return 0;
44 }