其实这题和poj中一个题目很像,这题是一些散点,求最大三角形面积,而poj里有一题是求两点之间最大距离!思路都是一样的,点都在凸包这线上,然后利用凸包公式,和多边形面积公式!
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
using namespace std;
const int maxn=50005;
int n;
struct point
{
double x;
double y;
double there;
} p[maxn],chs[maxn];
bool cmp1(point a,point b)
{
if(a.y==b.y)return a.x<b.x;
return a.y<b.y;
}
bool cmp2(point a,point b)
{
if(a.there==b.there)
return a.x<b.x;
return a.there<b.there;
}
double get_there(point a,point b)
{
return atan2((a.y-b.y),(a.x-b.x));
}
bool line_point1(point a,point b,point c)
{
double m;
m=(b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
if(m>0)return false;
if(m<0)return true;
if(m==0)return true;
return false;
}
double mul(point a,point b,point c)
{
double ans;
ans=(b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
return fabs(ans/2.0);
}
int main()
{
int i,j,k;
while(scanf("%d",&n)!=EOF)
{
for(i=0; i<n; i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
}
sort(p,p+n,cmp1);
for(i=0; i<n; i++)
{
p[i].there=get_there(p[i],p[0]);
}
sort(p+1, p+n,cmp2);
chs[0]=p[0];
chs[1]=p[1];
chs[2]=p[2];
int top=2;
for(i=3; i<n; i++)
{
while(top>=1&&line_point1(chs[top-1],chs[top],p[i]))
{
top--;
}
chs[++top]=p[i];
}
top++;
double max=0;
for(i=0; i<top; i++)
{
for(j=i+1; j<top; j++)
{
for(k=j+1; k<top; k++)
{
double sum;
sum=mul(chs[i],chs[j],chs[k]);
if(sum>max)
{
max=sum;
}
}
}
}
printf("%.2lf\n",max);
}
return 0;
}