zoukankan      html  css  js  c++  java
  • G

    Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, Lunits away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L).

    To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.

    Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to rocks (0 ≤ M ≤ N).

    FJ wants to know exactly how much he can increase the shortest distance *before* he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks.

    Input

    Line 1: Three space-separated integers: LN, and M 
    Lines 2.. N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position.

    Output

    Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing M rocks

    Sample Input

    25 5 2
    2
    14
    11
    21
    17

    Sample Output

    4

    Hint

    Before removing any rocks, the shortest jump was a jump of 2 from 0 (the start) to 2. After removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25).

    题意:小牛过河,可以移除M块石头,寻找所跳最小距离的最大值

    题解:1 —— L之间进行二分查找,如果存在当前位置last+mid > a[i] 说明此种方案存在更小的距离,要想保证此时的mid为最小距离就要将当前的石块去除,用cnt记录已经去除石块的个数,如果cnt > M(所给的能够去除石块的个数),则更新 right = mid - 1,否则 left = mid + 1;

    AC代码

     1 #include<stdio.h>
     2 #include<algorithm>
     3 
     4 using namespace std;
     5 
     6 
     7 int l, n, m;
     8 int a[50005];
     9 int solve(int x)
    10 {
    11     int cnt = 0;
    12     int last = 0;
    13     for(int i = 1; i <= n+1; i++)
    14     {
    15         if(a[i] < last + x)     //当前的位置加上跳的距离能够到达下一个石头
    16             cnt++;      //存在更小的跳跃距离
    17         else
    18             last = a[i];       //更新当前位置
    19     }
    20     return cnt;
    21 }
    22 
    23 int main()
    24 {
    25     int right, mid, left;
    26     while(~scanf("%d%d%d", &l, &n, &m))
    27     {
    28         for(int i = 1; i <= n; i++)
    29             scanf("%d", &a[i]);
    30         sort(a+1, a+n+1);
    31         a[n+1] = l;
    32         int ans = 0;
    33         right = l;
    34         left = 1;
    35         while(left <= right)
    36         {
    37             mid = (left + right) / 2;
    38             if(solve(mid) > m)
    39                 right = mid - 1;
    40             else
    41                 left = mid + 1;
    42         }
    43         printf("%d
    ", left - 1);
    44     }
    45     return 0;
    46 }
    View Code
    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    Map的两张遍历方法 keySet(),entrySet()
    http://localhost:8080请求用户名和密码。信息为:“XDB” 解决办法
    redis 集群出现的错误
    通信 缩略词
    redis
    hadoop 概念
    mysql 集群的一些概念
    SQL 基本关键字 函数 关联 连接
    JAVA集合
    SQL.Cookbook 读书笔记5 元数据查询
  • 原文地址:https://www.cnblogs.com/h-hkai/p/8589120.html
Copyright © 2011-2022 走看看