CodeForces 831D Office Keys

来源:互联网 发布:使命召唤ol有mac版吗 编辑:程序博客网 时间:2024/05/24 03:27

题目链接:http://codeforces.com/contest/831/problem/D
题意:有一个数轴,有n个人分别在ai的地方,你要去目的地p,但是去目的地之前必需去拿钥匙,有k把钥匙,每个人每秒只能移动一格,问你所有人都进到办公室需要最少需要多少时间
解析:由于不知道怎么想贪心策略,但是发现数据的规模并不大,n^2应该是能过的,而每个人只能对应一把钥匙,那么就直接排个序,然后枚举,拿长度为n的端去扫b数组,然后依次相减,维护最小值即可

#include <bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 20005;const int inf = 0x7fffffff;int a[maxn],b[maxn];int main(void){    int n,m,goal;    scanf("%d %d %d",&n,&m,&goal);    for(int i=0;i<n;i++)        scanf("%d",&a[i]);    for(int j=0;j<m;j++)        scanf("%d",&b[j]);    sort(a,a+n);    sort(b,b+m);    int ans = inf;    for(int i=0;i<m-n+1;i++)    {        int mx = 0;        for(int j=0;j<n;j++)            mx = max(mx,abs(a[j]-b[i+j])+abs(b[i+j]-goal));        ans = min(ans,mx);    }    printf("%d\n",ans);    return 0;}
原创粉丝点击