难在judge函数的编写,之前写了个复杂度n的judge函数。。。妥妥的TLE了
#include"cstdio"
#include"cmath"
#include"algorithm"
#include"cstring"
#define MAXN 500010
using namespace std;
int n,step,len,ans;
int stone[MAXN];
bool judge(int abi)
{ int temp=0,l,mark=0,t;
for(int i=0;i<=step;i++){
while(temp+abi>=stone[mark]&&mark<=n)
t=stone[mark++];
temp=t;
if(temp>=len) return true;
}
if(temp>=len) return true;
return false;
}
int get_ans()
{ int low=0,high=len,mid;
while(high>=low){
mid=low+(high-low)/2;
if(judge(mid)) high=mid-1;
else low=mid+1;
}
return mid;
}
int main()
{ while(scanf("%d%d%d",&len,&n,&step)!=EOF){
for(int i=0;i<n;i++) scanf("%d",&stone[i]);
stone[n]=len;
sort(stone,stone+n);
printf("%d\n",get_ans());
}
return 0;
}
HDU 4004:The annual Games(答案二分)
原文:http://www.cnblogs.com/luxiaoming/p/4705220.html