【推导】Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals) A. Office Keys

选择的钥匙一定是连续的,人和钥匙一定从左到右连续对应。

就枚举钥匙区间即可。

#include<cstdio>
#include<algorithm>
using namespace std;
int Abs(int x){
	return x<0 ? (-x) : x;
}
int n,K,p,a[1010],ans=2147483647,b[2010];
int main(){
	scanf("%d%d%d",&n,&K,&p);
	for(int i=1;i<=n;++i){
		scanf("%d",&a[i]);
	}
	for(int i=1;i<=K;++i){
		scanf("%d",&b[i]);
	}
	sort(a+1,a+n+1);
	sort(b+1,b+K+1);
	for(int i=1;i<=K;++i){
		if(i+n-1>K){
			break;
		}
		int maxx=0;
		for(int j=1,k=i;j<=n;++j,++k){
			maxx=max(maxx,Abs(a[j]-b[k])+Abs(b[k]-p));
		}
		ans=min(ans,maxx);
	}
	printf("%d
",ans);
	return 0;
}
原文地址:https://www.cnblogs.com/autsky-jadek/p/7169420.html