zoukankan      html  css  js  c++  java
  • 旋转卡壳法求最大三角形

    poj2079

    Triangle
    Time Limit: 3000MS   Memory Limit: 30000K
    Total Submissions: 8023   Accepted: 2368

    Description

    Given n distinct points on a plane, your task is to find the triangle that have the maximum area, whose vertices are from the given points.

    Input

    The input consists of several test cases. The first line of each test case contains an integer n, indicating the number of points on the plane. Each of the following n lines contains two integer xi and yi, indicating the ith points. The last line of the input is an integer −1, indicating the end of input, which should not be processed. You may assume that 1 <= n <= 50000 and −104 <= xi, yi <= 104 for all i = 1 . . . n.

    Output

    For each test case, print a line containing the maximum area, which contains two digits after the decimal point. You may assume that there is always an answer which is greater than zero.

    Sample Input

    3
    3 4
    2 6
    2 7
    5
    2 6
    3 9
    2 0
    8 0
    6 5
    -1

    Sample Output

    0.50
    27.00

    程序:

    #include"string.h"
    #include"stdio.h"
    #include"math.h"
    #include"stdlib.h"
    #define M 50001
    #define inf 999999999
    #define eps 1e-10
    typedef struct node
    {
        double x,y,cos,dis;
    }E;
    E p[M],q[M];
    double max(double a,double b)
    {
        return a>b?a:b;
    }
    int cmp(const void *a,const void *b)
    {
        if(fabs((*(struct node*)a).cos-(*(struct node*)b).cos)<eps)
            return (*(struct node*)a).dis>(*(struct node*)b).dis?1:-1;
        else
            return (*(struct node*)b).cos>(*(struct node*)a).cos?1:-1;
    }
    double angle(node p1,node p2)
    {
        double x1=p2.x-p1.x;
        double y1=p2.y-p1.y;
        double x2=1;
        double y2=0;
        return (x1*x2+y1*y2)/sqrt((x1*x1+y1*y1)*(x2*x2+y2*y2));
    }
    double pow(double x)
    {
        return x*x;
    }
    double Len(node p1,node p2)
    {
        return pow(p2.x-p1.x)+pow(p2.y-p1.y);
    }
    double cross(node p0,node p1,node p2)
    {
        double x1=p1.x-p0.x;
        double y1=p1.y-p0.y;
        double x2=p2.x-p0.x;
        double y2=p2.y-p0.y;
        return x1*y2-x2*y1;
    }
    int main()
    {
        int n,i,j,k;
        while(scanf("%d",&n),n!=-1)
        {
            int tep;
            E start;
            start.x=start.y=inf;
            for(i=0;i<n;i++)
            {
                scanf("%lf%lf",&p[i].x,&p[i].y);
                if(p[i].y<start.y)
                {
                    start=p[i];
                    tep=i;
                }
                else if(fabs(p[i].y-start.y)<eps)
                {
                    if(p[i].x<start.x)
                    {
                        start=p[i];
                        tep=i;
                    }
                }
            }
            p[tep].dis=0;
            p[tep].cos=10;
            for(i=0;i<n;i++)
            {
                if(i!=tep)
                {
                    p[i].cos=angle(start,p[i]);
                    p[i].dis=Len(start,p[i]);
                }
            }
            qsort(p,n,sizeof(p[0]),cmp);
            q[0]=p[n-1];
            q[1]=p[0];
            q[2]=p[1];
            int cnt=2;
            for(i=2;i<n;i++)
            {
                while(cross(q[cnt-1],q[cnt],p[i])<0)
                {
                    cnt--;
                }
                q[++cnt]=p[i];
            }//求凸包
            j=1;
            double s=0;
            j=k=1;
            for(i=0;i<cnt;i++)
            {
                while(cross(q[i],q[j%cnt],q[(k+1)%cnt])>cross(q[i],q[j%cnt],q[k%cnt]))
                    k++;
                s=max(s,cross(q[i],q[j%cnt],q[k%cnt]));
                while(cross(q[i],q[(j+1)%cnt],q[k%cnt])>cross(q[i],q[j%cnt],q[k%cnt]))
                    j++;
                s=max(s,cross(q[i],q[j%cnt],q[k%cnt]));
            }//注意cross()中i,j,k的书写顺序
            /*其思路是这样的,定点I,p,q,先I,p固定,让q旋转找到最大的面积三角形,之后,I,q固定,p旋转,
            找到最大的三角形面积,比较记录.然后i++;直到i遍历所有顶点.所求出来的三角形就是面积最大.
            这里的旋转卡壳思想就是固定,旋转.这样的.显然i++后,p,q两点不需要再从i+1,i+2开始,这个好
            形容,对p,q进行取模运算的时候,注意自己的SP栈指针多大.*/
            printf("%.2lf
    ",s/2);
        }
    }
    
    


  • 相关阅读:
    SpringMVC+Huploadify 实现文件上传下载
    删除代码中的注释
    shiro框架学习(二)
    shiro框架学习(三)
    shiro框架学习(一)
    数据库操作导入导出以及加快查询速度
    判断字符串中是否是数字
    分数判断
    异常处理的课后
    多态的课后总结
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348244.html
Copyright © 2011-2022 走看看