您的位置:首页 > 运维架构

POJ 3258 River Hopscotch 二分

2017-09-10 11:44 381 查看
原题链接

题意:牛要到河对岸,在与河岸垂直的一条线上,河中有N块石头,给定河岸宽度L,以及每一块石头离牛所在河岸的距离,

          现在去掉M块石头,要求去掉M块石头后,剩下的石头之间以及石头与河岸的最小距离的最大值。

对最小距离二分

对于每一次二分,设最小距离为m,若两石头i,j 距离不超过m,则移除石头j,否则下次从石头j开始计算距离。

判断移除石头数cnt与M关系进行下次二分。

#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=5e4+10;
int d[maxn];
int main(){
int L,N,M;
cin>>L>>N>>M;
for(int i=1;i<=N;i++) cin>>d[i];
sort(d+1,d+1+N);
d[0]=0,d[N+1]=L;
int l=0,r=L;
while(l<=r){
int cnt=0,m=(l+r)/2,ll=0;
//m: current minimum distance, cnt: total of rocks removed
for(int i=1;i<=N+1;i++){
if(d[i]-d[ll]<=m) cnt++;
//current distance is less than the minimum distance(m), remove No.i rock
else ll=i;
//satisfies condition, not remove, in next repeating i-ll=1, means no remove
}
if(cnt>M) r=m-1;
else l=m+1;
}
cout<<l<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm poj 二分