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;
    }
  • 相关阅读:
    Oracle 10gR2 Dataguard搭建(非duplicate方式)
    Linux scp 设置nohup后台运行
    Linux平台 Oracle 10gR2(10.2.0.5)RAC安装 Part3:db安装和升级
    Linux平台 Oracle 10gR2(10.2.0.5)RAC安装 Part2:clusterware安装和升级
    eclipse debug模式下总是自动跳到ThreadPoolExecutor.java类
    eclipse maven build、maven clean、maven install和maven test的区别 精析
    燕麦工作室第一卷:火力地堡高清下载
    java 泛型 精析
    任志强商学课:用企业家的思维理解商业 下载
    NodeJs编写小爬虫
  • 原文地址:https://www.cnblogs.com/jhz033/p/7202736.html
Copyright © 2011-2022 走看看