凸包(BZOJ1069)

时间:2021-01-20 20:11:37

顶点一定在凸包上,我们枚举对角线,观察到固定一个点后,随着另一个点的增加,剩下两个点的最优位置一定是单调的,于是就得到了一个优秀的O(n^2)做法。

#include <cstdio>
#include <algorithm> const int N = ;
int n,r,p1,p2,q[N];
double ans;
struct nd {
double x,y;
bool operator < (const nd &b) const {return x == b.x ? y < b.y : x < b.x;}
nd operator - (const nd &b) const {return (nd){x-b.x,y-b.y};}
double operator * (const nd &b) const {return x*b.y-y*b.x;}
}a[N];
double cj(int x, int y, int z) {return (a[x]-a[y])*(a[z]-a[y]);} int main() {
scanf("%d", &n);
for(int i = ; i <= n; i++) scanf("%lf%lf", &a[i].x, &a[i].y);
std::sort(a+, a++n);
for(int i = ; i <= n; i++) {
while(r > && cj(q[r], q[r-], i) >= ) r--;
q[++r] = i;
}
int rr = r;
for(int i = n-; i >= ; i--) {
while(r > rr && cj(q[r], q[r-], i) >= ) r--;
q[++r] = i;
}
for(int i = ; i < r; i++) {
int p1 = i+, p2 = i+;
for(int j = i+; j < r-; j++) {
while(p1 < j- && cj(q[j],q[i],q[p1]) <= cj(q[j],q[i],q[p1+])) p1++;
while(p2 <= j || (p2 < r- && cj(q[p2],q[i],q[j]) <= cj(q[p2+],q[i],q[j]))) p2++;
ans = std::max(ans, cj(q[j],q[i],q[p1])+cj(q[p2],q[i],q[j]));
}
}
printf("%.3f", ans/);
return ;
}