旋转卡壳法求最大三角形

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 −10 4 <= xi, yi <= 10 4 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\n",s/2);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值