【POJ2187】Beauty Contest【旋转卡壳】

【题目链接】

学习一发旋转卡壳。

/* Telekinetic Forest Guard */
#include 
#include 
#include 

using namespace std;

const int maxn = 50005;

int n, m;

struct Vector {
	int x, y;

	Vector(int a = 0, int b = 0) {
		x = a; y = b;
	}

	bool operator < (const Vector &a) const {
		return x != a.x ? x < a.x : y < a.y;
	}

	Vector operator - (const Vector &a) {
		return Vector(x - a.x, y - a.y);
	}
} p[maxn], cov[maxn];

inline int cross(Vector a, Vector b) {
	return a.x * b.y - a.y * b.x;
}

inline int dis(Vector a, Vector b) {
	return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}

inline void Andrew() {
	sort(p + 1, p + 1 + n);
	m = 0;
	for(int i = 1, k = 1; i <= n; i++) {
		for(; m > k && cross(cov[m] - cov[m - 1], p[i] - cov[m - 1]) <= 0; m--);
		cov[++m] = p[i];
	}
	for(int i = n - 1, k = m; i; i--) {
		for(; m > k && cross(cov[m] - cov[m - 1], p[i] - cov[m - 1]) <= 0; m--);
		cov[++m] = p[i];
	}
	if(n > 1) m--;
}

inline int rotcov() {
	int res = 0;
	for(int i = 1, j = 2; i <= m; i++) {
		for(; cross(cov[i + 1] - cov[i], cov[j] - cov[i]) < cross(cov[i + 1] - cov[i], cov[j + 1] - cov[i]); j == m ? j = 1 : j++);
		res = max(res, max(dis(cov[i], cov[j]), dis(cov[i + 1], cov[j + 1])));
	}
	return res;
}

int main() {
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) scanf("%d%d", &p[i].x, &p[i].y);
	Andrew();
	printf("%d\n", rotcov());
	return 0;
}


你可能感兴趣的:(计算几何)