BZOJ 1069 最大土地面积(旋转卡壳求最大四边形)

题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1069

题意:给出一个点,从中选出四个点组成的四边形面积最大。

思路:首先显然先求凸包。然后就是旋转卡壳。枚举矩形的对角线i和j,然后用旋转卡壳在其两侧记录到该直线的最大距离的两个点p1,p2。每次旋转p1和p2保持距离最大。

 

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <map>


#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define abs(x) ((x)>=0?(x):-(x))
#define i64 long long
#define u32 unsigned int
#define u64 unsigned long long
#define clr(x,y) memset(x,y,sizeof(x))
#define CLR(x) x.clear()
#define ph(x) push(x)
#define pb(x) push_back(x)
#define Len(x) x.length()
#define SZ(x) x.size()
#define PI acos(-1.0)
#define sqr(x) ((x)*(x))
#define MP(x,y) make_pair(x,y)
#define EPS 1e-10


#define FOR0(i,x) for(i=0;i<x;i++)
#define FOR1(i,x) for(i=1;i<=x;i++)
#define FOR(i,a,b) for(i=a;i<=b;i++)
#define FORL0(i,a) for(i=a;i>=0;i--)
#define FORL1(i,a) for(i=a;i>=1;i--)
#define FORL(i,a,b)for(i=a;i>=b;i--)


#define rush() int CC;for(scanf("%d",&CC);CC--;)
#define Rush(n)  while(scanf("%d",&n)!=-1)
using namespace std;


void RD(int &x){scanf("%d",&x);}
void RD(i64 &x){scanf("%lld",&x);}
void RD(u64 &x){scanf("%I64u",&x);}
void RD(u32 &x){scanf("%u",&x);}
void RD(double &x){scanf("%lf",&x);}
void RD(int &x,int &y){scanf("%d%d",&x,&y);}
void RD(i64 &x,i64 &y){scanf("%I64d%I64d",&x,&y);}
void RD(u32 &x,u32 &y){scanf("%u%u",&x,&y);}
void RD(double &x,double &y){scanf("%lf%lf",&x,&y);}
void RD(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);}
void RD(i64 &x,i64 &y,i64 &z){scanf("%I64d%I64d%I64d",&x,&y,&z);}
void RD(u32 &x,u32 &y,u32 &z){scanf("%u%u%u",&x,&y,&z);}
void RD(double &x,double &y,double &z){scanf("%lf%lf%lf",&x,&y,&z);}
void RD(char &x){x=getchar();}
void RD(char *s){scanf("%s",s);}
void RD(string &s){cin>>s;}

void PR(int x) {printf("%d\n",x);}
void PR(int x,int y) {printf("%d %d\n",x,y);}
void PR(i64 x) {printf("%lld\n",x);}
void PR(u32 x) {printf("%u\n",x);}
void PR(u64 x) {printf("%I64u\n",x);}
void PR(double x) {printf("%.3lf\n",x);}
void PR(char x) {printf("%c\n",x);}
void PR(char *x) {printf("%s\n",x);}
void PR(string x) {cout<<x<<endl;}


const int mod=12345678;
const i64 inf=((i64)1)<<60;
const double dinf=1e50;
const int INF=1000000005;
const int N=2005;


struct point
{
    double x,y;


    point(){}
    point(double _x,double _y)
    {
        x=_x;
        y=_y;
    }




    double operator*(point a)
    {
        return x*a.y-y*a.x;
    }




    void get()
    {
        scanf("%lf%lf",&x,&y);
    }


    void print()
    {
        printf("%lf %lf\n",x,y);
    }
};


point p[N],q[N];
int n,m;




int DB(double x)
{
    if(x>EPS) return 1;
    if(x<-EPS) return -1;
    return 0;
}


double Dis(point a,point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}




double cross(point a,point b,point p)
{
    return (b.x-a.x)*(p.y-a.y)-(b.y-a.y)*(p.x-a.x);
}


int cmp(point a,point b)
{
    double x=Dis(a,p[0]),y=Dis(b,p[0]);
    int flag=DB(cross(p[0],a,b));
    if(flag) return flag==1;
    return DB(x-y)<=0;
}


void Graham(point p[],int n,point q[],int &m)
{
    point temp;
    int i,k=0,a,b;
    for(i=1;i<n;i++)
    {
        a=DB(p[i].y-p[k].y);
        b=DB(p[i].x-p[k].x);
        if(a==-1||!a&&b==-1) k=i;
    }
    if(k!=0) temp=p[0],p[0]=p[k],p[k]=temp;
    sort(p+1,p+n,cmp);
    q[0]=p[0];
    q[1]=p[1];
    p[n]=p[0];
    m=2;
    for(i=2;i<=n;i++)
    {
        while(m>1&&DB(cross(q[m-2],q[m-1],p[i]))<=0) m--;
        q[m++]=p[i];
    }
    m--;
}


int OK(point a,point b,point p,point q)
{
    double A=fabs(cross(a,b,p));
    double B=fabs(cross(a,b,q));
    return A+EPS<B;
}


double cal(point p[],int n)
{
    p[n]=p[0];
    int p1,p2,i,j;
    double ans=0,temp;
    for(i=0;i<n;i++)
    {
        p1=(i+1)%n;
        p2=(i+2)%n;
        for(j=(i+2)%n;j!=i;j=(j+1)%n)
        {
            while(p1!=j&&OK(p[i],p[j],p[p1],p[p1+1])) p1=(p1+1)%n;
            while(p2!=i&&OK(p[i],p[j],p[p2],p[p2+1])) p2=(p2+1)%n;
            temp=fabs(cross(p[i],p[p1],p[j]))+fabs(cross(p[i],p[j],p[p2]));
            if(temp>ans) ans=temp;
        }
    }
    return ans/2;
}


int main()
{
    RD(n);
    int i;
    FOR0(i,n) p[i].get();
    Graham(p,n,q,m);
    PR(cal(q,m));
}

你可能感兴趣的:(ZOJ)