[POJ2079] Triangle

来源:互联网 发布:javascript图片轮播 编辑:程序博客网 时间:2024/05/01 04:14

题目描述

给定一个平面上的n个点,找出以这些点为顶点的三角形,面积最大的一个,输出它的面积。


输入格式

第一行一个整数n(1<=n<=50000)为点的个数
接下来的n行,每行包含两个整数xi和yi ( -10000<=xi,yi<=10000),表示当前这个点的坐标。


输出格式

输出一个数,为最大的三角形面积,精确到两位小数。


样例数据

样例输入

样例数据#1
3
3 4
2 6
2 7
样例数据#2
5
2 6
3 9
2 0
8 0
6 5

样例输出

样例数据#1
0.50
样例数据#2
27.00


题目分析

不就是把[SCOI2007] 最大土地面积的四边形去掉了一半吗。。。
枚举一般就行了
凸包上的点达不到50000,别被吓到了。


源代码

#include<algorithm>#include<iostream>#include<iomanip>#include<cstring>#include<cstdlib>#include<vector>#include<cstdio>#include<cmath>#include<queue>using namespace std;inline const int Get_Int() {    int num=0,bj=1;    char x=getchar();    while(x<'0'||x>'9') {        if(x=='-')bj=-1;        x=getchar();    }    while(x>='0'&&x<='9') {        num=num*10+x-'0';        x=getchar();    }    return num*bj;}const double eps=1e-10;int DoubleCompare(double x) { //精度三出口判断与0关系    if(fabs(x)<eps)return 0; //=0    else if(x<0)return -1; //<0    else return 1; //>0}struct Point {    double x,y;    bool operator < (Point b) const {        return x<b.x||(x==b.x&&y<b.y);    }};struct Vector {    double x,y;};Vector operator - (Point a,Point b) {    Vector c;    c.x=b.x-a.x;    c.y=b.y-a.y;    return c;}double Cross(Vector a,Vector b) { //叉积    return a.x*b.y-b.x*a.y;}double Area(Point a,Point b,Point c) { //三点的平行四边形有向面积    Vector u=b-a;    Vector v=c-a;    return Cross(u,v);}double Area(int n,Point* P) { //计算多边形有向面积(剖分法)    double ans=0;    for(int i=2; i<n; i++)ans+=Area(P[1],P[i],P[i+1]);    return ans/2;}int ConvexHull(int n,Point* p,Point* ans) { //Andrew算法求凸包(求上链与下链):p是点数组,ch是凸包顶点,返回顶点数    //输入不能有重复点,若要凸包边上没有输入点,将两个<=改为<    sort(p+1,p+n+1);    int top=0;    for(int i=1; i<=n; i++) {        while(top>1&&DoubleCompare(Cross(ans[top]-ans[top-1],p[i]-ans[top-1]))<=0)top--;        ans[++top]=p[i];    }    int k=top;    for(int i=n-1; i>=1; i--) {        while(top>k&&DoubleCompare(Cross(ans[top]-ans[top-1],p[i]-ans[top-1]))<=0)top--;        ans[++top]=p[i];    }    if(n>1)top--;    return top;}double Rotating_Calipers(int n,Point* p) { //旋转卡壳求最远点对距离    double ans=0;    p[n+1]=p[1];    for(int a=1; a<=n; a++) {        int Up=a%n+1;        for(int b=a+2; b<=n; b++) {            while(Up%n+1!=b&&abs(Area(p[a],p[b],p[Up]))<abs(Area(p[a],p[b],p[Up+1])))Up=Up%n+1;            ans=max(ans,abs(Area(p[a],p[b],p[Up])));        }    }    return ans;}//////////////Point a[50005],b[50005];int n;int main() {    n=Get_Int();    for(int i=1; i<=n; i++)scanf("%lf%lf",&a[i].x,&a[i].y);    int cnt=ConvexHull(n,a,b);    printf("%0.2lf\n",Rotating_Calipers(cnt,b)/2);    return 0;}

0 0
原创粉丝点击