【dp】codeforces 830-A Office Keys
Link:http://codeforces.com/problemset/problem/830/A
#include <bits/stdc++.h>
using namespace std;
/*
830-A Office Keys
題意:n個人,k把鑰匙,每個人需要找到一把鑰匙,同時行動到辦公室,
求所有人都到最少需要的時間。
題解:貪心想一下,如果最右邊的人取了最右邊的鑰匙,是否其他人取這個鑰匙更好。
我們知道其實最右邊的人取的鑰匙的位置,一定在他左邊的人取的鑰匙的位置的右邊。
那麼我們需要排序下,再動規下,dp[i][j]代表前i個取範圍1-j鑰匙的最少花費時間。
*/
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF=0x3f3f3f3f;
const int N=2010;
const int mod = 1e9+7;
int a[N],b[N];
int dp[N][N];
int main()
{
int n,m,p;
scanf("%d%d%d",&n,&m,&p);
for(int i = 0; i < n; i++)
scanf("%d",&a[i]);
sort(a,a+n);
for(int i = 0; i < m; i++)
scanf("%d",&b[i]);
sort(b,b+m);
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
dp[i][j] = INF*2;
for(int i = 0; i < n; i++)
{
for(int j = i; j < m; j++)
{
if(i == 0){
dp[i][j] = abs(a[i]-b[j])+abs(b[j]-p);
if(j != 0)
dp[i][j] = min(dp[i][j],dp[i][j-1]);
}
else{
dp[i][j] = min(dp[i][j-1],max(dp[i-1][j-1],abs(a[i]-b[j])+abs(b[j]-p)));
}
//printf("%d %d: %d\n",i,j,dp[i][j]);
}
}
printf("%d\n",dp[n-1][m-1]);
return 0;
}
相關文章
- codeforces 148 D 概率dp
- codeforces455A Boredom (裸DP)
- Codeforces 372B Counting Rectangles is Fun:dp套dp
- CodeForces - 628D (數位dp)
- Codeforces 180C Letter:dp
- POJ1160 Post Office[序列DP]
- Codeforces 158E Phone Talks:dp
- Codeforces 432D Prefixes and Suffixes:KMP + dpKMP
- Codeforces 294B Shaass and Bookshelf:dp
- 【dp】codeforces 837D Round Subset
- 【dp】CodeForces - 623B Array GCDGC
- codeforces 148 D Bag of mice(概率dp)
- CodeForces 401D 數位DP
- Codeforces 486D Valid Sets:Tree dp【n遍O(n)的dp】
- 【數位dp】Beautiful numbers CodeForces - 55D
- Codeforces Round #358 (Div. 2) D dp
- Codeforces 459E Pashmak and Graph:dp + 貪心
- Codeforces 148D Bag of mice (概率dp)
- Codeforces Round #316 (Div. 2) E dp
- Codeforces Round #321 (Div. 2) D 狀壓dp
- Codeforces 461B Appleman and Tree:Tree dpAPP
- Codeforces 366C Dima and Salad:揹包dp
- Codeforces 478D Red-Green Towers:dp
- codeforces 341C Iahub and Permutations(組合數dp)
- Puzzles CodeForces 696B 樹形DP 期望計算
- Codeforces Round #336 (Div. 2) D 區間dp
- Codeforces 245H Queries for Number of Palindromes:區間dp
- Codeforces 455B A Lot of Games:博弈dp【多局遊戲】GAM遊戲
- Codeforces Round #336 (Div. 2) C 二分+dp
- Codeforces 219D Choosing Capital for Treeland:Tree dpAPI
- Codeforces 429B Working out:dp【列舉交點】
- Codeforces 571B Minimization:dp + 貪心【前後相消】
- Codeforces 11D A Simple Task 題解 [ 藍 ] [ 狀壓 dp ]
- C - Keys
- Codeforces 869C The Intriguing Obsession:組合數 or dpGUISession
- Codeforces 461B. Appleman and Tree[樹形DP 方案數]APP
- Codeforces Round #360 (Div. 2) E dp 類似01揹包
- Codeforces 463D Gargari and Permutations:隱式圖dp【多串LCS】3D