poj 3258 River Hopscotch

#include <iostream>            //二分法,参照poj 3273
#include <algorithm>
using namespace std;
int main()
{
int list[50010],L,N,M,i;
scanf(
"%d%d%d",&L,&N,&M);
list[
0]=0;list[N+1]=L;
for(i=1;i<=N;++i)
{
scanf(
"%d",&list[i]);
}
sort(list,list
+N+2);
int l=0,r=L,mid,res;
while(l<r)
{
int count=0,s=0;
mid
=l+(r-l+1)/2; //不是mid=(l+r)/2;也不是mid=l+(r-l)/2;
//这一步至关重要,如l=3,r=4,mid=3,会一直执行l=mid,无限循环,而mid=l+(r-l+1)/2;保证了每次待查找序列的规模都会变小
for(i=1;i<=N+1;++i)
{
if(s+list[i]-list[i-1]>=mid)
s
=0;
else
{
count
++;
s
+=list[i]-list[i-1];
}
if(count>M)
break;
}
if(count>M)
r
=mid-1;
else
l
=mid;
}
printf(
"%d\n",l);
return 0;
}

//对应于 1100序列中最右侧的1的查找(最大值):
//http://duanple.blog.163.com/blog/static/709717672009049528185/

  

posted on 2011-07-22 15:12  sysu_mjc  阅读(140)  评论(0编辑  收藏  举报

导航