POJ 3258 River Hopscotch(二分答案)
Posted shenben
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 3258 River Hopscotch(二分答案)相关的知识,希望对你有一定的参考价值。
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 21939 | Accepted: 9081 |
Description
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 M 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
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
Sample Input
25 5 2 2 14 11 21 17
Sample Output
4
Hint
【题意】
在起点和终点之间,有 N 块岩石(不含起点和终点的岩石),至多可以从起点和终点之间移走 M 块岩石(不能移走起点和终点的岩石),求移走岩石后最短跳跃距离的最大值
【分析】
[NOIP2015跳石头(原题)]
直接二分,具体详见代码
【代码】
#include<cstdio> #include<algorithm> #include<iostream> #define debug(x) cerr<<#x<<" "<<x<<‘ ‘; 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 ",ans); return 0; }
以上是关于POJ 3258 River Hopscotch(二分答案)的主要内容,如果未能解决你的问题,请参考以下文章