二分法出现MLE错误,各位大佬帮忙看看吖
查看原帖
二分法出现MLE错误,各位大佬帮忙看看吖
702186
saltwater楼主2022/3/27 21:47
import java.io.*;

public class Main {
	
	static int N=1000000+5;
	static int n,m;
	static int hightest=-1;
	
	static int tree[]=new int[N];
	
	public static boolean check(int mid){
		long res=0;
		for(int i=0;i<n;i++) {
			if(tree[i]>=mid) res+=(tree[i]-mid);//砍树也是有限制的
			
			if(res>=m) {
				return true;
			}
		}
		
		return false;
	}
	
	public static void main(String args[]) throws IOException{
		
		BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
		String init[]=in.readLine().split(" ");
		
		n=Integer.parseInt(init[0]);
		m=Integer.parseInt(init[1]);
		
		String data[]=in.readLine().split(" ");
		for(int i=0;i<n;i++) {
			tree[i]=Integer.parseInt(data[i]);
			hightest=Math.max(tree[i],hightest);
		}

		int l=0;
		int r=hightest;
		
		while(l<r) {
			int mid=(l+r+1)/2;
			
			if(check(mid)) l=mid;
			else r=mid-1;
		}
		
		System.out.println(l);
	}
}

第三个测试点出现MLE错误

2022/3/27 21:47
加载中...