【POJ2187】Beauty Contest【旋转卡壳】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/BraketBN/article/details/51507582

【题目链接】

学习一发旋转卡壳。

/* Telekinetic Forest Guard */
#include <cstdio>
#include <cstring>
#include <algorithm>

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;
}


阅读更多
换一批

没有更多推荐了,返回首页