zoukankan      html  css  js  c++  java
  • codeforce830A. Office Keys

    A. 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.

    题意

    一条数轴,n个人,k把钥匙, 和一间在 p 的屋子。每个人拿上

    每个人和每把 key 都有个坐标.,每个人每秒能走一单位的距离。每个人都要拿一把 key,,然后走进屋子。俩人不能拿同一把 key,求所有人都走到屋子的最小时间。

    分析

    首先当然是拿离自己近的钥匙,所以可以先把钥匙和人按坐标排序,然后要求所有人走进屋子的时间最少,显然可以二分。

    所以先二分时间,判断是否满足即可。

    code

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<cmath>
     4 #include<algorithm>
     5 #include<iostream>
     6 
     7 using namespace std;
     8 
     9 const int MAXN = 2010;
    10 typedef long long LL;
    11 
    12 LL r[MAXN],k[MAXN];
    13 bool vis[MAXN];
    14 LL n,m,pos;
    15 
    16 int read()
    17 {
    18     int x = 0,f = 1;char ch = getchar();
    19     while (ch<'0'||ch>'9') {if (ch=='-') f = -1; ch = getchar(); }
    20     while (ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+ch-'0'; ch = getchar(); }
    21     return x*f;
    22 }
    23 bool check(LL x)
    24 {
    25     int cnt = 0;
    26     memset(vis,0,sizeof(vis));
    27     for (int i=1; i<=n; ++i)
    28         for (int j=1; j<=m; ++j)//人与钥匙都是从左边枚举 
    29         {
    30             if (vis[j]) continue ;//钥匙已经拿了 
    31             if (abs(r[i]-k[j])+abs(k[j]-pos)<=x)//满足条件 
    32             {
    33                 vis[j] = true;
    34                 cnt++;
    35                 break; //只要有一个即满足条件,break 
    36             }
    37         }
    38     if (cnt==n) return true;
    39     return false;
    40 }
    41 int main()
    42 {
    43 
    44     n = read();m = read();pos = read();
    45     for (int i=1; i<=n; ++i)
    46         r[i] = read();
    47     for (int i=1; i<=m; ++i)    
    48         k[i] = read();
    49 
    50     sort(r+1,r+n+1);
    51     sort(k+1,k+m+1);
    52     LL mid,l = 0,r = 1e10,ans;
    53 
    54     while (l<=r)//二分时间 
    55     {
    56         mid = (l+r)>>1;
    57         if (check(mid))
    58         {
    59             ans = mid;
    60             r = mid-1;
    61         }
    62         else l = mid+1;
    63     }
    64     cout<<ans;
    65     return 0;
    66 }
  • 相关阅读:
    Yarn安装与配置
    如何找到所有 HTML select 标签的选中项
    Jquery 获取某个样式除第一个以外的元素
    常用的字符串截取方法
    App的埋点测试
    手写 promise
    关于JSON.parse(JSON.stringify(obj))实现深拷贝应该注意的坑
    计算一个数组的深度
    数组的 交集 差集 补集 并集
    webpack -- element-ui 的按需引入
  • 原文地址:https://www.cnblogs.com/mjtcn/p/7323930.html
Copyright © 2011-2022 走看看