您的位置:首页 > 其它

HDU 3934 Summer holiday(求点集的最大三角形面积)

2014-10-17 22:19 429 查看
HDU 3934 Summer holiday(求点集的最大三角形面积)
http://acm.hdu.edu.cn/showproblem.php?pid=3934
题意:

给你一个n个点的点集,然后要你找出这个点集中的最大三角形面积?

分析:

首先n<=100W,所以如果暴力枚举肯定超时.

最大三角形的三个顶点 肯定是 这个点集的凸包的三个顶点. 如果有哪个点不是凸包的顶点,那么可以将该点换成对应方向的凸包顶点使得三角形面积变大.

所以我们只需要求出凸包(凸包的顶点数远远小于100W),然后枚举凸包上的任意三点面积即可.

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

//精度控制
const double eps=1e-10;
int dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    return x<0?-1:1;
}

//点
struct Point
{
    double x,y;
    Point(){}
    Point(double x,double y):x(x),y(y){}
    bool operator==(const Point &rhs)const
    {
        return dcmp(x-rhs.x)==0 && dcmp(y-rhs.y)==0;
    }
    bool operator<(const Point &rhs)const
    {
        return dcmp(x-rhs.x)<0 || (dcmp(x-rhs.x)==0 && dcmp(y-rhs.y)<0);
    }
};

//向量
typedef Point Vector;

//点-点==向量
Vector operator-(Point A,Point B)
{
    return Vector(A.x-B.x,A.y-B.y);
}

//叉积
double Cross(Vector A,Vector B)
{
    return A.x*B.y-A.y*B.x;
}

//三角形面积
double Area(Point A,Point B,Point C)
{
    return fabs(Cross(A-B,C-B))/2;
}

//求构成凸包的所有点,保存在ch中
int ConvexHull(Point *p,int n,Point *ch)
{
    sort(p,p+n);
    n=unique(p,p+n)-p;
    int m=0;
    for(int i=0;i<n;i++)
    {
        while(m>1 && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2])<=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-2])<=0) m--;
        ch[m++]=p[i];
    }
    if(n>1) m--;
    return m;
}

const int maxn=1000000+5;
Point p[maxn],ch[maxn];
int main()
{
    int n;
    while(scanf("%d",&n)==1)
    {
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        int m=ConvexHull(p,n,ch);

        double ans=0;//最大面积
        for(int i=0;i<m;i++)//枚举所有可能的三角形面积
        for(int j=i+1;j<m;j++)
        for(int k=j+1;k<m;k++)
            ans=max(ans,Area(ch[i],ch[j],ch[k]));
        printf("%.2lf\n",ans);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: