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.

  • 相关阅读:
    [bzoj1072][SCOI2007][排列perm] (状态压缩+数位dp+排列去重)
    [bzoj2461][BeiJing2011][符环] (括号配对+记忆化搜索+高维dp)
    [bzoj4027][HEOI2015][兔子与樱花] (树形dp思想+玄学贪心)
    [bzoj1925][Sdoi2010][地精部落] (序列动态规划)
    [bzoj1004][HNOI2008][Cards] (置换群+Burnside引理+动态规划)
    [bzoj1003][ZJOI2006][物流运输] (最短路+dp)
    [spoj104][Highways] (生成树计数+矩阵树定理+高斯消元)
    [bzoj1002][FJOI2007 轮状病毒] (生成树计数+递推+高精度)
    [bzoj 1001][Beijing2006]狼抓兔子 (最小割+对偶图+最短路)
    [模拟赛FJOI Easy Round #2][T3 skill] (最小割+最大权闭合子图(文理分科模型))
  • 原文地址:https://www.cnblogs.com/bolderic/p/7192873.html
Copyright © 2011-2022 走看看