HDU 3934 (旋转卡壳)

题意是给你100w个点,求面积最大的三角形。

三角形的三个点显然必选是凸包的顶点,先求出凸包。

然后100w个点怎么想都想不出nlgn的算法,然后n^2的居然过了,然后看了下网上的题解居然有n^3过的也是瞎了狗眼。

n^2的可以这么搞,暴力枚举两个点,然后第三个点用旋转卡壳寻找,所以总体复杂度是n^2。

坑爹的是我用了double居然MLE,然后所有的double改成了float就好了。

#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef unsigned long long ll;
#define maxn 1111111
#define pi acos (-1)
#define rotate Rotate

const float eps = 1e-8;
int dcmp (float x) {
    if (fabs (x) < eps)
        return 0;
    else return x < 0 ? -1 : 1;
}
struct point {
    float x, y;
    point (float _x = 0, float _y = 0) : x(_x), y(_y) {}
    point operator - (point a) const {
        return point (x-a.x, y-a.y);
    }
    point operator + (point a) const {
        return point (x+a.x, y+a.y);
    }
    bool operator < (const point &a) const {
        return x < a.x || (x == a.x && y < a.y);
    }
    bool operator == (const point &a) const {
        return dcmp (x-a.x) == 0 && dcmp (y-a.y) == 0;
    }
};

point operator * (point a, float p) {
    return point (a.x*p, a.y*p);
}
float cross (point a, point b) {
    return (float)a.x*b.y-(float)a.y*b.x;
}
float dot (point a, point b) {
    return a.x*b.x + a.y*b.y;
}

int n, m, tot;
point p[maxn], ch[maxn];

int ConvexHull () {
    sort (p, p+n);
    int m = 0;
    for (int i = 0; i < n; i++) {
        while (m > 1 && cross (ch[m-1]-ch[m-2], p[i]-ch[m-1]) <= 0)
            m--;
        ch[m++] = p[i];
    }
    int k = m;
    for (int i = n-2; i >= 0; i--) {
        while (m > k && cross (ch[m-1]-ch[m-2], p[i]-ch[m-1]) <= 0)
            m--;
        ch[m++] = p[i];
    }
    if (n > 1)
        m--;
    return m;
}

float rotate_calipers () {
    if (m == 1 || m == 2)
        return 0;
    float ans = 0;
    int cur = 1;
    for (int add = 1; add < m; add++) {
        for (int i = 0; i < m; i++) {
            while (cross (ch[i]-ch[(i+add)%m], ch[(cur+1)%m]-ch[cur]) < 0)
                cur = (cur+1)%m;
            float res = max (cross (ch[(i+add)%m]-ch[i], ch[cur]-ch[i]), cross (ch[(i+add)%m]-ch[i], ch[(cur+1)%m]-ch[i]));
            ans = max (ans, res);
        }
    }
    return ans/2.0;
}

int main () {
    //freopen ("in", "r", stdin);
    while (scanf ("%d", &n) == 1 ) {
        for (int i = 0; i < n; i++) {
            scanf ("%f%f", &p[i].x, &p[i].y);
        }
        m = ConvexHull ();
        printf ("%.2f\n", rotate_calipers ());
    }
    return 0;
}


你可能感兴趣的:(HDU 3934 (旋转卡壳))