您的位置:首页 > 其它

Codeforces 831 D Office Keys

2017-07-15 14:09 405 查看
题目地址:http://codeforces.com/contest/831/problem/D

题意: 办公室被锁住了,每个人必须去拿钥匙才能打开门,有n个人,m把钥匙,让你求出n个人全部进办公室的最短时间是多少。

思路:对人以及钥匙的位置进行排序,然后因为一定要去到办公室,所以说钥匙一定是连续的,因为最后都要去到办公室,那段距离想要竟可能短肯定是竟可能接近,所以遍历开始钥匙的起点再求n个人的区间,直接暴力就好了。

#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <cstdio>
#include <algorithm>
#define LL long long
#define N 2010
#define M 50010
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
const LL mod = 1e9 + 7;
const double eps = 1e-9;
LL a
, b
;
int main() {
cin.sync_with_stdio(false);
int n, m, q;
while (cin >> n >> m >> q) {
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < m; i++) {
cin >> b[i];
}
sort(a, a + n);
sort(b, b + m);
LL ans, sum;
ans = inf;
for (int i = 0; i <= m - n; i++) {
sum = 0;
for (int j = 0; j < n; j++) {
sum = max(sum, abs(a[j] - b[j + i]) + abs(b[j + i] - q));
}
ans = min(sum, ans);
}
cout << ans << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: