二分答案模板

二分答案模板_第1张图片

//找最小值
//从不满足到满足的情况,求满足的最小值,即:大于等于key的最小值
//不满足 mid key 满足
while(left<=right){
	    	int  mid = (left+right)>>1;
	    	if(check(mid)){
	    		ans = mid;
	    		right = mid -1;
	    	}else{
	    		left = mid +1;
	    	}
	    }
	    System.out.println(ans);
//找最大值
//从满足到不满足的情况,求满足的最大值,即:小于等于key的最大值
//满足 key mid 不满足
  while(left<=right){
	    	int  mid = (left+right)>>1;
	    	if(check(mid)){
	    		ans = mid;
	    		left = mid +1;
	    	}else{
	    		right = mid -1;
	    	}
	    }
	    System.out.println(ans);

你可能感兴趣的:(算法竞赛)