[POJ2079]Triangle(计算几何-旋转卡壳-最远点对)

本文介绍了一种通过枚举顶点来寻找给定点集中构成的最大三角形面积的算法。使用了凸包技巧来减少搜索范围,并通过旋转卡壳算法优化求解过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

我是超链接

题意:

给出一些点,求顶点在这些点上的面积最大的三角形

题解:

枚举一个点,在上次的基础上找另两个点,这样发现另两个点似乎也是单调的,没什么特别的,暴力去找就行了

代码:

#include <cstdio>
#include <algorithm>
using namespace std;
const double INF=1e18;
const double eps=1e-9;
int dcmp(double x)
{
    if (x<=eps && x>=-eps) return 0;
    return (x>0)?1:-1;
}
struct po
{
    double x,y;
    po (double X=0,double Y=0){x=X; y=Y;}
}d[50005],sta[50005];
int n,top;double ans;
bool operator <(const po &a,const po&b){return a.x<b.x||(a.x==b.x&&a.y<b.y);}
double cj(po x,po y){return x.x*y.y-x.y*y.x;}
po operator -(po x,po y){return po(x.x-y.x,x.y-y.y);}
void tb()
{
    sort(d+1,d+n+1);
    top=0;
    for (int i=1;i<=n;i++)
    {
        while (top>1 && dcmp(cj(sta[top]-sta[top-1],d[i]-sta[top-1]))<=0) top--;
        sta[++top]=d[i];     
    }
    int k=top;
    for (int i=n-1;i>=1;i--)
    {
        while (top>k && dcmp(cj(sta[top]-sta[top-1],d[i]-sta[top-1]))<=0) top--;
        sta[++top]=d[i];
    }
    if (n>1) top--;
}
void rotating()
{
    int j=2,k=3;
    sta[top+1]=sta[1];
    for (int i=1;i<=top;i++)
    {
        while (dcmp(cj(sta[j]-sta[i],sta[k]-sta[i]) - cj(sta[j+1]-sta[i],sta[k]-sta[i]))<0) j=j%top+1;
        ans=max(ans,cj(sta[j]-sta[i],sta[k]-sta[i])/2);

        while (dcmp(cj(sta[j]-sta[i],sta[k]-sta[i]) - cj(sta[j]-sta[i],sta[k+1]-sta[i]))<0) k=k%top+1;
        ans=max(ans,cj(sta[j]-sta[i],sta[k]-sta[i])/2);
    }
}
int main()
{
    while (scanf("%d",&n) && n!=-1)
    {
        double x,y;ans=-INF;
        for (int i=1;i<=n;i++)
        {
            scanf("%lf%lf",&x,&y);
            d[i]=po(x,y);
        }
        tb();
        rotating();
        printf("%.2lf\n",ans);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值