二分其實很好考慮對吧,時間越多,滿足的越多,如何judge呢,這個仔細想想還是挺有意思的.
1.左邊的人拿左邊的鑰匙能在這個時間段滿足,那就滿足,對於整體方案來說是最優的
2.如果左邊的對於某把鑰匙不滿足,之後出現了某個位置鑰匙滿足,那麼之前那把鑰匙一定是在這個人的左邊,那麼進而證明右邊的人也一定不會用到這把不滿足的鑰匙.
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long LL;
const int Maxn = 2e3 + 7;
int n, k;
LL a[Maxn], b[Maxn], p;
bool Judge(LL Max){
bool flag;
int j=0;
for(int i=0;i<n;i++){
flag = false;
while(j<k){
if((abs(a[i]-b[j]) + abs(b[j]-p)) <= Max){
flag = true;
j++;
break;
}
j++;
}
if(!flag) return false;
}
return true;
}
void solve(){
LL Left = 0, Right = 1e14;
while(Left < Right){
LL mid = Left + (Right - Left) / 2;
if(Judge(mid)) Right = mid;
else Left = mid + 1;
}
printf("%I64d\n", Left);
}
int main(){
scanf("%d%d%I64d", &n, &k, &p);
for(int i=0;i<n;i++) scanf("%I64d", &a[i]);sort(a, a+n);
for(int i=0;i<k;i++) scanf("%I64d", &b[i]);sort(b, b+k);
solve();
return 0;
}