AC日记——830A - Office Keys
思路:
背包;
代码:
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn 1005 #define maxk 2005 #define ll long long int n,k,p; ll dp[maxn][maxk],ai[maxn],bi[maxk]; inline void in(int &now) { char Cget=getchar();now=0; while(Cget>'9'||Cget<'0') Cget=getchar(); while(Cget>='0'&&Cget<='9') { now=now*10+Cget-'0'; Cget=getchar(); } } inline void in(ll &now) { char Cget=getchar();now=0; while(Cget>'9'||Cget<'0') Cget=getchar(); while(Cget>='0'&&Cget<='9') { now=now*10+Cget-'0'; Cget=getchar(); } } inline ll check() { //memset(dp,127/3,sizeof(dp)); for(int i=1;i<=n;i++) for(int v=1;v<=k;v++) dp[i][v]=max(dp[i-1][v-1],abs(ai[i]-bi[v])+abs(bi[v]-p)); ll Min=2e9; for(int i=n;i<=k;i++) Min=min(dp[n][i],Min); return Min; } int main() { in(n),in(k),in(p); for(int i=1;i<=n;i++) in(ai[i]); for(int i=1;i<=k;i++) in(bi[i]); sort(ai+1,ai+n+1),sort(bi+1,bi+k+1); cout<<check(); return 0; }