[BZOJ1069][SCOI2007]最大土地面积(凸包+旋转卡壳)

题目描述

传送门

题解

感觉这样的题就是寻找题目的单调性。。。
枚举一个对角线,然后在对角线的两边分别选一个点,这两个点满足单调
时间复杂度 O(n2)
这道题卡时非常丧病
能用叉积的尽量用叉积,少用比如说DisTL之类的函数
调用控制精度的dcmp也会慢一丢丢

代码

#include
#include
#include
#include
#include
#include
using namespace std;
#define N 10005

const double eps=1e-9;
int dcmp(double x)
{
    if (x<=eps&&x>=-eps) return 0;
    return (x>0)?1:-1;
}
struct Vector
{
    double x,y;
    Vector(double X=0,double Y=0)
    {
        x=X,y=Y;
    }
    bool operator < (const Vector &a) const
    {
        return xtypedef Vector Point;
Vector operator - (Vector a,Vector b) {return Vector(a.x-b.x,a.y-b.y);}

int n,top;
double ans;
Point p[N],stack[N];

double Dot(Vector a,Vector b)
{
    return a.x*b.x+a.y*b.y;
}
double Cross(Vector a,Vector b)
{
    return a.x*b.y-a.y*b.x;
}
double Len(Vector a)
{
    return sqrt(Dot(a,a));
}
void graham()
{
    sort(p+1,p+n+1);
    top=0;
    for (int i=1;i<=n;++i)
    {
        while (top>1&&dcmp(Cross(stack[top]-stack[top-1],p[i]-stack[top-1]))<=0)
            --top;
        stack[++top]=p[i];
    }
    int k=top;
    for (int i=n-1;i>=1;--i)
    {
        while (top>k&&dcmp(Cross(stack[top]-stack[top-1],p[i]-stack[top-1]))<=0)
            --top;
        stack[++top]=p[i];
    }
    if (n>1) --top;
}
double rotating()
{
    double ans=0;
    int i,j,k,l;
    for (i=1;i<=top;++i)
    {
        k=i%top+1;
        l=(i+2)%top+1;
        for (j=i+2;j<=top;++j)
        {
            while (k%top+1!=j&&dcmp(fabs(Cross(stack[i]-stack[j],stack[k]-stack[i]))-fabs(Cross(stack[i]-stack[j],stack[k%top+1]-stack[i])))<=0)
                k=k%top+1;
            while (l%top+1!=i&&dcmp(fabs(Cross(stack[i]-stack[j],stack[l]-stack[i]))-fabs(Cross(stack[i]-stack[j],stack[l%top+1]-stack[i])))<=0)
                l=l%top+1;
            ans=max(ans,fabs(Cross(stack[i]-stack[j],stack[k]-stack[i]))/2+fabs(Cross(stack[i]-stack[j],stack[l]-stack[i]))/2);
        }
    }
    return ans;
}
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;++i) scanf("%lf%lf",&p[i].x,&p[i].y);
    graham();
    ans=rotating();
    printf("%.3lf\n",ans);
}

你可能感兴趣的:(题解,省选,计算几何)