最大三角形
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2613 Accepted Submission(s): 871
Problem Description
老师在计算几何这门课上给Eddy布置了一道题目,题目是这样的:给定二维的平面上n个不同的点,要求在这些点里寻找三个点,使他们构成的三角形拥有的面积最大。
Eddy对这道题目百思不得其解,想不通用什么方法来解决,因此他找到了聪明的你,请你帮他解决这个题目。
Eddy对这道题目百思不得其解,想不通用什么方法来解决,因此他找到了聪明的你,请你帮他解决这个题目。
Input
输入数据包含多组测试用例,每个测试用例的第一行包含一个整数n,表示一共有n个互不相同的点,接下来的n行每行包含2个整数xi,yi,表示平面上第i个点的x与y坐标。你可以认为:3 <= n <= 50000 而且 -10000 <= xi, yi <= 10000.
Output
对于每一组测试数据,请输出构成的最大的三角形的面积,结果保留两位小数。
每组输出占一行。
每组输出占一行。
Sample Input
3 3 4 2 6 3 7 6 2 6 3 9 2 0 8 0 6 6 7 7
Sample Output
1.50 27.00
Author
Eddy
Recommend
lcy
题解:找凸包,在凸包点上找面积最大的三角形,照模板写的....
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define PI 3.14159265
#define inf 0.000001
struct point{
double x,y;
}p[50005],res[50005];
int n;
double MAX(double a,double b)
{
return a>b?a:b;
}
double mult(struct point p1,struct point p2,struct point p3)
{
return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);
}
double dis(struct point a,struct point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double jueduizhi(double a)
{
return a>0?a:-a;
}
double S(struct point p1,struct point p2,struct point p3)
{
return jueduizhi((p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x))/2;
}
int cmp(const void *a,const void *b)
{
struct point c=*(struct point *)a;
struct point d=*(struct point *)b;
double k=mult(p[0],c,d);
if(k<0||(!k&&dis(c,p[0])>dis(d,p[0]))) return 1;
return -1;
}
int convex()
{
int i,top=1;
struct point temp;
for(i=1;i<n;i++)
{
if(p[i].y<p[0].y||(p[i].y==p[0].y&&p[i].x<p[0].x))
temp=p[i],p[i]=p[0],p[0]=temp;
}
qsort(p+1,n-1,sizeof(p[0]),cmp);
res[0]=p[0],res[1]=p[1];
for(i=2;i<n;i++)
{
while(top>=1&&mult(res[top-1],res[top],p[i])<=0) top--;
res[++top]=p[i];
}
return top;
}
int main()
{
double sum;
int i,j,k,top;
while(scanf("%d",&n)>0)
{
for(i=0;i<n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
top=convex();
for(sum=i=0;i<=top;i++)
{
for(j=i+1;j<=top;j++)
{
for(k=j+1;k<=top;k++)
{
sum=MAX(sum,S(res[i],res[j],res[k]));
}
}
}
printf("%.2lf\n",sum);
}
return 0;
}