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

题目:

我是超链接

题意:

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

题解:

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

代码:

#include 
#include 
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.xdouble 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);
    }
}

你可能感兴趣的:(计算几何)