zoukankan      html  css  js  c++  java
  • NYOJ619 青蛙过桥(二分 + 贪心)

     

    青蛙过桥

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:3
     
    描述
    The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. One test in the Ironfrog Triathlon is jumping. This project requires the frog athletes to jump over the river. The width of the river is L (1<= L <= 1000000000). There are n (0<= n <= 500000) stones lined up in a straight line from one side to the other side of the river. The frogs can only jump through the river, but they can land on the stones. If they fall into the river, they are out. The frogs was asked to jump at most m (1<= m <= n+1) times. Now the frogs want to know if they want to jump across the river, at least what ability should they have. (That is the frog's longest jump distance).
     
    输入
    The input contains several cases. The first line of each case contains three positive integer L, n, and m.
    Then n lines follow. Each stands for the distance from the starting banks to the nth stone, two stone appear in one place is impossible.
    输出
    For each case, output a integer standing for the frog's ability at least they should have.
    样例输入
    6 1 2
    2
    25 3 3
    11 
    2
    18
    样例输出
    4
    11
    来源
    hdu
    题意:
        简单的说就是给你一段长度,在这一段中给出m个点,然后在这m个点中选出k个点,
    让这k个点之间相邻两个点的之间距离的最大值最小(与上一题的不同点)
    思路:通过二分枚举这个最小值,然后通过贪心的思想找出满足要求的最小的这个最大值
     1 #include <cstdio>
     2 #include <iostream>
     3 #include <algorithm>
     4 
     5 using namespace std;
     6 
     7 int len, n, m;
     8 int pos[500005];
     9 
    10 bool judge(int k)
    11 {
    12     int st = 0;
    13     int cnt = 0;
    14     for(int i = 0; i <= n; ++i)
    15     {
    16         /*
    17         if(pos[i] - st > k)
    18         {
    19             ++cnt;
    20             if(cnt > m)
    21                 return false;
    22             st = pos[i-1];
    23         }*/
    24         if(pos[i] - st <= k)
    25         {
    26             while(pos[i] - st <= k && i <= n)
    27                 ++i;
    28             st = pos[--i];
    29             ++cnt;
    30         }    
    31     }
    32     if(cnt <= m)
    33         return true;
    34     return false;
    35 }
    36 
    37 int binary_search()
    38 {
    39     
    40     int left = 0;
    41     int right = len;
    42     
    43     for(int i = 1; i <= n; ++i)
    44         if(pos[i] - pos[i-1] > left)
    45             left = pos[i] - pos[i-1];
    46     
    47     
    48     int mid = (left+right)>>1;
    49     while(left <= right)
    50     {
    51         if(judge(mid)) 
    52             right = mid-1;
    53         else           // mid 太小 
    54             left = mid+1;
    55         mid = (left+right)>>1;
    56     }
    57     return right + 1; 
    58 }
    59 
    60 int main()
    61 {
    62     while(~scanf("%d%d%d", &len, &n, &m))
    63     {
    64         for(int i = 0; i < n; ++i)
    65             scanf("%d", &pos[i]);
    66         pos[n] = len;
    67         sort(pos, pos+n);
    68         printf("%d\n", binary_search());
    69     }    
    70     return 0;
    71 }
     
  • 相关阅读:
    ESLint 规则详解(一)
    我为什么不看好微信小程序
    两分钟实现安全完备的登录模块
    基于大数据的用户行为预测在前端性能优化上的应用
    《深入浅出NodeJS》mindmap
    如何设计一个异步Web服务——任务调度
    如何设计一个异步Web服务——接口部分
    【技能】眼镜清洗,如何不用眼镜布让眼镜一尘不染?
    【技能】小白耳机维修入门--各种耳机插头接线图--耳机维修汇总贴
    【电脑skill】怎么关闭wps热点?永久关闭wps右下角弹窗的方法!
  • 原文地址:https://www.cnblogs.com/dongsheng/p/3107091.html
Copyright © 2011-2022 走看看