【POJ 2187】Beauty Contest 凸包+旋转卡壳

xuán zhuǎn qiǎ ké模板题

是这么读吧(≖ ‿ ≖)✧

算法挺简单:找对踵点即可,顺便更新答案。

#include<cstdio>
#include<cstring>
#include<algorithm>
#define read(x) x=getint()
#define max(a,b) (a)>(b)?(a):(b)
#define N 50003
using namespace std;
inline int getint() {
	int k = 0, fh = 1; char c = getchar();
	for(; c < '0' || c > '9'; c = getchar())
		if (c == '-') fh = -1;
	for(; c >= '0' && c <= '9'; c = getchar())
		k = k * 10 + c - '0';
	return k * fh;
}
inline int sqr(int x) {
	return x * x;
}
struct Point {
	int x, y;
	Point(int _x = 0, int _y = 0) : x(_x), y(_y) {}
} a[N], tu[N];
Point operator - (Point a, Point b) {
	return Point(a.x - b.x, a.y - b.y);
}
inline int Cross(Point a, Point b) {
	return a.x * b.y - a.y * b.x;
}

int n, top = 0;
inline bool cmp(Point X, Point Y) {
	return X.y == Y.y ? X.x < Y.x : X.y < Y.y;
}
inline void mktb() {
	 for(int i = 1; i <= n; ++i) {
	 	while (top > 1 && Cross(tu[top] - tu[top - 1], a[i] - tu[top]) <= 0)
	 		--top;
	 	tu[++top] = a[i];
	 }
	 int k = top;
	 for(int i = n - 1; i > 0; --i) {
	 	while (top > k && Cross(tu[top] - tu[top - 1], a[i] - tu[top]) <= 0)
	 		--top;
	 	tu[++top] = a[i];
	 }
}
int main() {
	read(n);
	for(int i = 1; i <= n; ++i)
		read(a[i].x), read(a[i].y);
	sort(a + 1, a + n + 1, cmp);
	mktb();
	int nxt = 2, ans = 0;
	for(int i = 1; i < top; ++i) {
		while (Cross(tu[i + 1] - tu[i], tu[nxt + 1] - tu[i]) > Cross(tu[i + 1] - tu[i], tu[nxt] - tu[i])) {
			++nxt;
			if (nxt == top)
				nxt = 1;
		}
		ans = max(ans, sqr(tu[i].x - tu[nxt].x) + sqr(tu[i].y - tu[nxt].y));
	}
	printf("%d\n", ans);
	return 0;
}

更新求凸包的模板,之前那个太麻烦了hhh

转载于:https://www.cnblogs.com/abclzr/p/5352329.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值