难在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; }
时间: 2024-11-02 19:47:13