zoukankan      html  css  js  c++  java
  • CF Round 424 D Offices Keys (贪心)

    题目链接:http://codeforces.com/contest/831/problem/D

    思路:由于拿到钥匙之后需要走的路程是确定的,因此关键在于如何选择钥匙。对于最靠近P位置的人来说,拿最接近P且同侧的钥匙是最划算的。

    因此二分答案,每次check的时候从小到大判断对于所有的人是否都能在时限内进入office即可。

    另外也可以使用动态规划 dp[i][j]:前j个钥匙中i个人拿走i把所需的时间,则

    dp[i][j] = max(dp[i - 1][j - 1], abs(a[i] - b[j])+abs(b[j] - p))          i == j

    dp[i][j] = min(dp[i][j - 1], max(dp[i - 1][j - 1], abs(a[i] - b[j])+abs(b[j] - p)))    i < j

    需要注意无论二分还是dp都需要先排序

    代码:

    二分:

     1 #define maxn 2010
     2 int n, k, p;
     3 int a[maxn], b[maxn];
     4 
     5 bool check(long long x){
     6     int st = 0;
     7     for(int i = 0; i < n; i++){
     8         bool flag = false;
     9         for(int j = st; j < k; j++){
    10             if(abs(p - b[j]) + abs(a[i] - b[j]) <= x){
    11                 flag = true;
    12                 st = j + 1;
    13                 break;
    14             }
    15         }
    16         if(!flag) return false;
    17     }
    18     return true;
    19 } 
    20 
    21 
    22 int main(){
    23     scanf("%d %d %d", &n, &k, &p);
    24     for(int i = 0; i < n; i++){
    25         scanf("%d", &a[i]);
    26     }
    27     for(int i = 0; i < k; i++){
    28         scanf("%d", &b[i]);
    29     }
    30     sort(a, a + n);
    31     sort(b, b + k);
    32     long long l = 0, r = 2e15;
    33     while(l < r){
    34         long long mid = (l + r) / 2;
    35         if(check(mid)){
    36             r = mid;
    37         }
    38         else{
    39             l = mid + 1;
    40         }
    41     }
    42     printf("%lld
    ", l);
    43 }

    DP:

     1 #define maxn 2010
     2 #define inf 0x3f3f3f3f3f3f3f3f
     3 int n, k, p;
     4 long long a[maxn], b[maxn];
     5 long long dp[maxn][maxn];
     6 
     7 int main(){
     8     scanf("%d %d %d", &n, &k, &p);
     9     for(int i = 1; i <= n; i++){
    10         scanf("%lld", &a[i]); 
    11     }
    12     for(int i = 1; i <= k; i++){
    13         scanf("%lld", &b[i]);
    14     }
    15     memset(dp, 0, sizeof(dp));
    16     sort(a + 1, a + n + 1);
    17     sort(b + 1, b + k + 1);
    18     
    19     for(int i = 1; i <= n; i++){
    20         for(int j = i; j <= k; j++){
    21             if(i == j){
    22                 dp[i][j] = max(dp[i - 1][j - 1], abs(a[i] - b[j]) + abs(p - b[j]));
    23             } 
    24             else dp[i][j] = min(dp[i][j - 1], max(dp[i - 1][j - 1], abs(p - b[j]) + abs(a[i] - b[j])));
    25 //            debug
    26 //            printf("%d %d:%lld
    ", i, j, dp[i][j]);
    27         }
    28     }
    29     long long ans = dp[n][k];
    30     for(int i = n; i <= k; i++){
    31         ans = min(ans, dp[n][i]);
    32     }
    33     printf("%lld
    ", ans);
    34 } 

    题目:

    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.

  • 相关阅读:
    错误1053:服务没有及时响应启动或控制请求
    错误号码2003 Can't connect to MySQL server 'localhost' (0)
    分析slow-log 每小时慢sql情况
    用spring annotation声明的bean,当打包在jar中时,无法被扫描到
    mysql 锁查看
    linux 使用expect
    tomcat启动报错:java.lang.ClassNotFoundException:org.springframework.web.context.ContextLoaderListener
    Spring LDAP authenticate method with Pooling
    一步步教你使用Proguard混淆Java源代码
    JAVA获取CLASSPATH路径
  • 原文地址:https://www.cnblogs.com/bolderic/p/7192873.html
Copyright © 2011-2022 走看看