Codeforces 831 D Office Keys

题目地址: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[N], b[N];
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值