|洛谷|二分|P1316 丢瓶盖

http://www.luogu.org/problem/show?pid=1316

二分答案后贪心地check即可

#include
#include
#include
#include
#define ms(i,j) memset(i,j, sizeof i);
using namespace std;
int a,b;
int x[100005];
int l = 1,r = 0;
bool check(int mid)
{
     int last=1,k=1;
     for (int i=2;i<=a;i++)
     {
         if (x[i]-x[last]>=mid)
         {
            k++;
            last = i; 
         }
     }
     if(k=l)
    {
          int mid = (l+r)/2;
          if (check(mid)) l = mid+1;
          else r = mid-1;
    }
    printf("%d\n", l-1);
    return 0;
}


你可能感兴趣的:(二分/分治,洛谷)