Codeforces Round #276 (Div. 2) B. Valuable Resources 二分

由于边界与坐标轴平行,则只需确定正方形的左下角和边长即可确定正方形。

而左下角可以根据mines的坐标确定,所以只需二分枚举正方形的边长即可,给定边长,O(n)时间内可以检查是否满足。

代码如下:

#include <cstdio>
#define N 1005
#define INF 1000000001
int x[N], y[N], n, orig_x = INF, orig_y = INF;

bool check(int len){
	int right = orig_x + len, up = orig_y + len;
	for(int i = 0; i < n; i ++){
		if(!(x[i] >= orig_x && x[i] <= right && y[i] >= orig_y && y[i] <= up))
			return false;
	}
	return true;
}

int main(){
	scanf("%d", &n);
	for(int i = 0; i < n; i ++){
		scanf("%d %d", &x[i], &y[i]);
		if(x[i] < orig_x)
			orig_x = x[i];
		if(y[i] < orig_y)
			orig_y = y[i];
	}
	int l = 0, r = 2 * INF;
	while(l < r){
		int mid = l + ((r - l) >> 1);
		if(check(mid))
			r = mid;
		else
			l = mid + 1;
	}
	printf("%I64d\n", (long long)l * l);
	return 0;
}


你可能感兴趣的:(codeforces,二分)