码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3258 River Hopscotch(二分答案)

时间:2019-01-23 23:28:58      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:placed   题意   ESS   mes   instead   this   mission   more   font   

River Hopscotch
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 21939   Accepted: 9081

Description

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.

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).

【题意】

在起点和终点之间,有 N 块岩石(不含起点和终点的岩石),至多可以从起点和终点之间移走 M 块岩石(不能移走起点和终点的岩石),求移走岩石后最短跳跃距离的最大值

 

【分析】

[NOIP2015跳石头(原题)] 

直接二分,具体详见代码

 

【代码】

#include<cstdio>
#include<algorithm>
#include<iostream>
#define debug(x) cerr<<#x<<" "<<x<<‘\n‘;
using namespace std;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
const int N=1e5+5;
int ans,n,m,L,d[N];
inline bool check(int now){
    int res=0,p=0;//p为前一个石头到指向石头的距离 
    for(int i=1;i<=n;i++){
        if(d[i]-p<now){//如果第i个石头到前一个石头的距离小于假设的答案
            res++;//该石头可以搬走 
        }
        else{//如果大于(即满足条件) 
            p=d[i];//则保留该石头,并将p记录 下一个石头到该石头的距离 
        } 
    }
    return res<=m;
}
int main(){
    L=read(),n=read(),m=read();
    for(int i=1;i<=n;i++) d[i]=read();d[++n]=L;//我们假设在终点处有第n+1个石头,则他到起点的距离为L 
    sort(d+1,d+n+1);//保证石头有序 
    int l=0,r=L,mid;
    while(l<=r){
        mid=l+r>>1;
        if(check(mid)){//说明二分结果小了,要向右二分
            ans=mid;//记录答案 
            l=mid+1;
        }
        else{
            r=mid-1;//说明结果大了,要向左二分
        }
    }
    printf("%d\n",ans);
    return 0;
}


 

 

 

POJ 3258 River Hopscotch(二分答案)

标签:placed   题意   ESS   mes   instead   this   mission   more   font   

原文地址:https://www.cnblogs.com/shenben/p/10311923.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!