poj3258二分法

/*对rock间的距离进行二分*/
#include<iostream>
#include <algorithm>
#define Max 50005
using namespace std;
int rock[Max];
int l,n,m,mn;
bool judge(int x)//判断可移除的rock是否大于m
{
 int del=0;
 for (int i=1,j=0;i<=n+1;)
 {
  if(rock[i]-rock[j]<=x)
  {
   del++;i++;
  }
  else
  {
   j=i;i++;
  }
 }
 if(del>m)
  return true;//right=mid-1;
 else
  return false;//left=mid+1; 
}
int main()

 while(cin>>l>>n>>m)
 {
  rock[0]=0;rock[n+1]=l;
  for (int i=1;i<=n;i++)
  {
   scanf("%d",&rock[i]);
   if(mn>rock[i])
    mn=rock[i];
  }
  sort(rock+1,rock+n+1);
  int right=l,left=mn;  
  while(left<=right)
  {
   int mid=(right+left)>>1;
   if (judge(mid))
   {
    right=mid-1;
   }
   else
   {
    left=mid+1;
   }
  }
  cout<<left<<endl;
 }
 return 0;
}

posted @ 2011-09-07 10:36  qijinbiao1  阅读(409)  评论(0编辑  收藏  举报