zoukankan      html  css  js  c++  java
  • River Hopscotch (二分)

    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, L units 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.

    输入

    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.

    输出

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

    样例输入

     

    25 5 2
    2
    14
    11
    21
    17

    样例输出

    4

     

    提示

    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).
     
    多做做二分的题目就会找到感觉
     
     1 #include <iostream>
     2 #include <cstring>
     3 #include <algorithm>
     4 using namespace std;
     5 typedef long long ll;
     6 ll L,N,M;
     7 ll arr[50005];
     8 
     9 bool check(ll mid){ ///二分一个最短距离 比较要扔掉的个数和实际要扔掉的个数
    10     ll num=0,sum=0;
    11     for(int i=1;i<=N+1;i++){
    12         sum+=arr[i]-arr[i-1];
    13 //        if(sum<=mid) num++;   //以这个距离去移除
    14         if(sum<mid) num++;
    15 ///如过这个石头符合就把他人掉 看看他之前的之后的是不是符合  所以用sum求和
    16         else{
    17             sum=0;
    18         }
    19     }
    20 //    return num<=M;
    21     return num>M;
    22 }
    23 
    24 ll Binary(ll left,ll right){
    25     ll ans;   ///这个是移除的最短距离
    26     while(left<=right){
    27         ll mid=left+right>>1;
    28         if(check(mid)) right=mid-1;   ///移除多了
    29         else left=mid+1,ans=mid;
    30     }
    31     return ans;
    32 }
    33 
    34 int main(){
    35     ios::sync_with_stdio(false);
    36     cin>>L>>N>>M;
    37     ll maxx=0;
    38     arr[0]=0;
    39     for(int i=1;i<=N;i++) cin>>arr[i];
    40     arr[N+1]=L;
    41     sort(arr+1,arr+N+1+1);
    42     cout << Binary(1,arr[N+1]) << endl;  //最短距离从一开始
    43     return 0;
    44 }
    View Code
  • 相关阅读:
    Multi-level CSS3 UL_LI Dropdown Menu
    10个优秀的 HTML5 & CSS3 下拉菜单制作教程
    MVC中@Html.Action的用法(类似自定义控件)
    ASP.NET MVC---自定义HtmlHelper方法
    FormsAuthentication登录ReturnUrl使用绝对路径
    asp.net mvc 身份验证中返回绝对路径的ReturnUrl
    asp.net 2.0 下的表单验证Cookieless属性
    IHttpModule在webconfig中的注册
    中国象棋棋谱浏览器
    刘永富-微信助手
  • 原文地址:https://www.cnblogs.com/qq-1585047819/p/11260788.html
Copyright © 2011-2022 走看看