题目链接:https://www.luogu.com.cn/problem/P2678
对两个石头之间的最短距离mid进行二分枚举,初始时left=0,right=起点到终点的距离,
mid=(l=r)>>1,当移除石头的数量大于n时,right=mid-1mid=(l=r)>>1,当移除石头的数量大于n时,left=mid最终mid就是石头之间的最短距离 03.具体实现 具体代码
public class Main {
static int[] data;
static int l,n,m;
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
l=in.nextInt();//l:起点到终点的距离
n=in.nextInt();//n:起点到终点的岩石数
m=in.nextInt();//m:移走的石头数
data=new int[n+1];
for (int i = 1; i <= n; i++) {
data[i]=in.nextInt();
}
int res=0;
int left=1,right=l;
while (left<=right) {
int mid=(left+right)>>1;//mid是石头的之间的最小距离(两个石头之间的距离>=mid)
if(judge(mid)) {
res=mid;
left=mid+1;
}else {
right=mid-1;
}
}
System.out.println(res);
}
//当移除石头数量<=m时,返回true
private static boolean judge(int mid) {
//num为长度为mid时最终移除的石头的数量 now可以看作当前站在第几个石头上 i可以看作是前面的石头
int num=0,i=0,now=0;
while (i
DeBug分析
04.深入练习
https://www.luogu.com.cn/problem/CF371Chttps://www.luogu.com.cn/problem/P1314
05.二分套路总结
https://blog.csdn.net/qq_46237746/article/details/123813308


![Java解P2678 [NOIP2015 提高组] 跳石头,有图有注释,通俗易懂 Java解P2678 [NOIP2015 提高组] 跳石头,有图有注释,通俗易懂](http://www.mshxw.com/aiimages/31/785243.png)
