跳石头深悉
#include<stdio.h> #include<algorithm> using namespace std; int m,n; long long l,map[50015],left,right,mid; int main() { scanf("%lld %d %d",&l,&n,&m); //if(n==0||m==0) {printf("%lld\n",l);return 0;} for(int i=1;i<=n;i++) scanf("%lld",&map[i]); map[n+1]=l; left=0; right=l; while(left<=right)//可以等于,left=3,right=4时,mid=3,再进行一次后mid=4 mid=(left+right)/2; printf("mid=%lld\n",mid); int move=0,h=0; for(int i=1;i<=n+1;i++) { if(map[i]-h<mid)move++; else h=map[i]; } if(move<=m) //如果发现石块刚好满足也要再往后找一找啊。。。万一下个也可以 { left=mid+1;printf("left1=%lld\n",left);} else { right=mid-1; printf("right1=%lld\n",right); } printf("left=%lld\n",left); printf("right=%lld\n",right); } printf("%lld",left-1); //为什么不输出left——因为看上面left=mid+1,而你二分的是mid---跳跃长度,显然left+1后并没有保证满足条件(下一个万一只更改了right就跳出呢?) return 0; }