poj2079Triangle(N点中三点组成三角形面积最大)

时间:2023-11-09 21:16:02

链接

根据旋转卡壳的思想,找到当前边的最远点。

确定i,j找到最远的k使 cross(i,j,k)最大,那么i,j+1时只需从k+1开始找即可 。

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 50010
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
struct point
{
int x,y;
point(int x=,int y=):x(x),y(y) {}
} p[N],ch[N];
typedef point pointt;
pointt operator - (point a,point b)
{
return point(a.x-b.x,a.y-b.y);
}
int dcmp(double x)
{
if(fabs(x)<eps) return ;
return x<?-:;
}
double cross(point a,point b)
{
return 1.0*a.x*b.y-1.0*a.y*b.x;
}
double getarea(point a,point b,point c)
{
return fabs(cross(b-a,c-a))/;
}
double mul(point p0,point p1,point p2)
{
return cross(p1-p0,p2-p0);
}
double dis(point a)
{
return sqrt(1.0*a.x*a.x+a.y*a.y);
}
bool cmp(point a,point b)
{
if(dcmp(mul(p[],a,b))==) return dis(a-p[])<dis(b-p[]);
return dcmp(mul(p[],a,b))>;
}
int graham(int n)
{
int i,k=,top;
point tmp;
for(i = ; i< n; i++)
{
if(p[i].y<p[k].y||(p[i].y==p[k].y&&p[i].x<p[k].x))
k = i;
}
if(k!=)
swap(p[],p[k]);
sort(p+,p+n,cmp);
ch[] = p[];
ch[] = p[];
top = ;
for(i = ; i < n; i++)
{
while(top>&&dcmp(mul(ch[top-],ch[top],p[i]))<=)
top--;
ch[++top] = p[i];
}
return top;
}
int main()
{
int n,i,j,k,kk;
while(scanf("%d",&n)!=EOF)
{
if(n==-) break;
for(i = ; i < n; i++)
scanf("%d%d",&p[i].x,&p[i].y);
int top = graham(n);
double maxz=,area;
++top;
ch[top] = ch[];
for(i=; i<top; ++i)
{
j=(i+)%top;
k=(j+)%top;
while(k!=i && getarea(ch[i],ch[j],ch[k])<getarea(ch[i],ch[j],ch[k+]))
k=(k+)%top;
maxz = max(maxz,getarea(ch[i],ch[j],ch[k]));
if(k == i)
continue;
kk=(k+)%top;
while(j!=kk && k!=i)
{
area=getarea(ch[i],ch[j],ch[k]);
if(area>maxz)
area=maxz;
while(k!=i && getarea(ch[i],ch[j],ch[k])<getarea(ch[i],ch[j],ch[k+]))
k=(k+)%top;
maxz = max(maxz,getarea(ch[i],ch[j],ch[k]));
j=(j+)%top;
}
}
printf("%.2f\n",maxz); }return ;
}