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

时间:2022-03-13 19:09:59

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