BZOJ 1069: [SCOI2007]最大土地面积 凸包,旋转卡壳

来源:互联网 发布:sql多张表合并 union 编辑:程序博客网 时间:2024/06/06 04:05

Description

  在某块平面土地上有N个点,你可以选择其中的任意四个点,将这片土地围起来,当然,你希望这四个点围成
的多边形面积最大。
Input

  第1行一个正整数N,接下来N行,每行2个数x,y,表示该点的横坐标和纵坐标。
Output

  最大的多边形面积,答案精确到小数点后3位。
Sample Input
5

0 0

1 0

1 1

0 1

0.5 0.5
Sample Output
1.000
HINT

数据范围 n<=2000, |x|,|y|<=100000
解题方法: 很显然这四个点都是凸包上的。枚举对角线,然后维护一下另外两个顶点,发现另外两个顶点是单调的,用旋转卡壳的思想很好实现。然后旋转卡壳可以看这个博客:见这里
代码如下:

#include <bits/stdc++.h>using namespace std;const double eps = 1e-8;const int maxn = 2005;int n, top;double ans;struct point{    double x, y;    point(){}    point(double x, double y) : x(x), y(y) {}}p[maxn], s[maxn];inline double dcmp(double a){ //判断a和0的大小关系    if(fabs(a) <= eps) return 0;    else return a > 0 ? 1 : -1;}inline double dis(point a, point b){    return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);}inline point operator -(point a, point b){ //-    return point(a.x - b.x, a.y - b.y);}inline double operator *(point a, point b){//xj    return a.x * b.y - a.y * b.x;}inline bool operator <(point a, point b){ //排序关键字,按顺指针排序    int t = dcmp((p[1] - a) * (p[1] - b));    if(t == 0) return dis(p[1], a) < dis(p[1], b);    else return t > 0;}inline void solve(){ //qiu tu    int t = 1;    for(int i = 2; i <= n; i++){        if(p[i].y < p[t].y || (p[i].y == p[t].y && p[i].x < p[t].x)) t = i;    }    swap(p[t], p[1]);    sort(p + 2, p + n + 1);    s[++top] = p[1]; s[++top] = p[2];    for(int i = 3; i <= n; i++){        while(top > 1 && dcmp((p[i] - s[top - 1]) * (s[top] - s[top - 1])) >= 0) top--;        s[++top] = p[i];    }}//mei ju dui jiao xian, xuan zhuan ka keinline void work(){//xuan zhuan ka ke    s[top + 1] = s[1];    int a, b;    for(int x = 1; x <= top - 2; x++){//枚举对角线端点1        a = x % top + 1; //2        b = (x + 2) % top + 1; //4        for(int y = x + 2; y <= top; y++){//枚举对角线端点2            while(a % top + 1 != y && dcmp((s[a + 1] - s[x]) * (s[y] - s[x]) - (s[a] - s[x]) * (s[y] - s[x])) > 0) a = a % top + 1;            while(b % top + 1 != x && dcmp((s[y] - s[x]) * (s[b + 1] - s[x]) - (s[y] - s[x]) * (s[b] - s[x])) > 0) b = b % top + 1;            ans = max(ans, (s[a] - s[x]) * (s[y] - s[x]) + (s[y] - s[x]) * (s[b] - s[x]));        }    }}int main(){    scanf("%d", &n);    for(int i = 1; i <= n; i++){        scanf("%lf%lf", &p[i].x, &p[i].y);    }    solve();    work();    printf("%.3f\n", ans / 2.0);    return 0;}
0 0