zoukankan      html  css  js  c++  java
  • Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) D. Office Keys time limit per test2 seconds 二分

    D. Office Keys
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.

    You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.

    Input

    The first line contains three integers nk and p (1 ≤ n ≤ 1 000, n ≤ k ≤ 2 000, 1 ≤ p ≤ 109) — the number of people, the number of keys and the office location.

    The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — positions in which people are located initially. The positions are given in arbitrary order.

    The third line contains k distinct integers b1, b2, ..., bk (1 ≤ bj ≤ 109) — positions of the keys. The positions are given in arbitrary order.

    Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.

    Output

    Print the minimum time (in seconds) needed for all n to reach the office with keys.

    Examples
    input
    2 4 50
    20 100
    60 10 40 80
    output
    50
    input
    1 2 10
    11
    15 7
    output
    7
    Note

    In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50seconds. Thus, after 50 seconds everybody is in office with keys.

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<stack>
    #include<cstring>
    #include<vector>
    #include<list>
    #include<set>
    #include<map>
    using namespace std;
    #define LL long long
    #define pi (4*atan(1.0))
    #define eps 1e-14
    #define bug(x)  cout<<"bug"<<x<<endl;
    const int N=1e5+10,M=2e6+10,inf=1e9+10;
    const LL INF=1e18+10,mod=1e9+7;
    
    int n,k;
    LL a[N],b[N],p;
    LL dis(LL s,LL mid,LL e)
    {
        return abs(mid-s)+abs(e-mid);
    }
    int check(LL x)
    {
        int s=1,flag=1;
        for(int i=1;i<=n;i++)
        {
            while(s<=k&&dis(a[i],b[s],p)>x)s++;
            if(s>k){
                flag=0;
                break;
            }
            s++;
        }
        if(!flag)return 0;
        return 1;
    }
    int main()
    {
        scanf("%d%d%lld",&n,&k,&p);
        for(int i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        for(int i=1;i<=k;i++)
            scanf("%lld",&b[i]);
        sort(a+1,a+1+n);
        sort(b+1,b+1+k);
        LL s=0,e=1e10,ans=-1;
        while(s<=e)
        {
            LL mid=(s+e)>>1;
            if(check(mid))
            {
                ans=mid;
                e=mid-1;
            }
            else s=mid+1;
        }
        printf("%lld
    ",ans);
        return 0;
    }
  • 相关阅读:
    bootstrap-table 切换页码保留勾选的checkbox
    vsftp服务器同步文件
    MySQL5.7 主从复制配置
    VMware NAT模式下设置网络
    在Jsp中调用静态资源,路径配置问题,jsp获取路径的一些方法
    centos7.3安装配置vsftp
    Linux安装配置Nginx
    jsp登录页面,展示错误信息,刷新页面后错误依然存在解决方案
    linux中使用Jmeter压测总结
    常规测试方法
  • 原文地址:https://www.cnblogs.com/jhz033/p/7202736.html
Copyright © 2011-2022 走看看