最大土地面积 HYSBZ - 1069 旋转卡壳

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/leekerian/article/details/82118371
#include  <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>

using namespace std;


const int maxn=66666;
const double eps=1e-8;

int sgn(double x)
{
    if(fabs(x)<eps)
        return 0;
    if(x<0)
        return -1;
    else
        return 1;
}
struct Point
{
    double x,y;
    Point(){}
    Point(double _x,double _y)
    {
        x=_x;
        y=_y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x-b.x,y-b.y);
    }
    double operator ^(const Point &b)const
    {
        return x*b.y-y*b.x;
    }
    double operator *(const Point &b)const
    {
        return x*b.x+y*b.y;
    }
};

Point p[maxn];
int n;
int Stack[maxn];
int top;

double dist(Point a,Point b)
{
    return sqrt((a-b)*(a-b));
}
bool cmp(Point a,Point b)
{
    int ans=sgn((a-p[0])^(b-p[0]));
    if(ans==1)
        return true;
    else if(ans==0)
        return dist(a,p[0])<dist(b,p[0]);
    else
        return false;
}

void graham()
{
    for(int i=0;i<n;i++)
    {
        if(p[i].x<p[0].x||(p[i].x==p[0].x&&p[i].y<p[0].y))
            swap(p[0],p[i]);
    }
    sort(p+1,p+n,cmp);
    if(n==1)
    {
        Stack[0]=0;
        top=1;
    }
    else if(n==2)
    {
        Stack[0]=0;
        Stack[1]=1;
        top=2;
    }
    else
    {
        Stack[0]=0;
        Stack[1]=1;
        top=2;
        for(int i=2;i<n;i++)
        {
            while(top>1&&sgn((p[Stack[top-1]]-p[Stack[top-2]])^(p[i]-p[Stack[top-2]]))<=0)
                top--;
            Stack[top++]=i;
        }
    }
}
Point p1[maxn];

double dist1(Point a,Point b)
{
    return (a-b)*(a-b);
}
double rot_all()
{
    int l;
    int l1;
    double res=0;
    int cur=1;
    for(int i=0;i<top;i++)
    {
        l=1;
        l1=1;
        for(int j=i+2;j<top;j++)
        {
            while(sgn(((p1[l]-p1[i])^(p1[j]-p1[i]))-((p1[(l+1)%top]-p1[i])^(p1[j]-p1[i])))<=0)
                l=(l+1)%top;
            while(sgn(((p1[j]-p1[i])^(p1[l1]-p1[i]))-((p1[j]-p1[i])^(p1[(l1+1)%top]-p1[i])))<=0)
                l1=(l1+1)%top;
            res=max(res,(((p1[l]-p1[i])^(p1[j]-p1[i]))+((p1[j]-p1[i])^(p1[l1]-p1[i]))));
        }
    }
    return res;
}


int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
    {
        scanf("%lf %lf",&p[i].x,&p[i].y);
    }
    graham();
    for(int i=0;i<top;i++)
    {
        p1[i]=p[Stack[i]];
        //cout<<p1[i].x<<" "<<p1[i].y<<endl;
    }
    double ans;
    ans=rot_all();
    printf("%.3lf\n",ans/2.0);
    return 0;
}
/*
8
0 0
1 3
2 4
3 3
5 0
3 -3
2 -4
1 -3
*/

猜你喜欢

转载自blog.csdn.net/leekerian/article/details/82118371