最大三角形
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4044 Accepted Submission(s): 1447
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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 50003
#define eps 1e-10
using namespace std;
struct vector {
double x,y;
vector (double X=0,double Y=0) {
x=X,y=Y;
}
}a[N],ch[N];
typedef vector point;
vector operator -(vector a,vector b){
return vector (a.x-b.x,a.y-b.y);
}
vector operator +(vector a,vector b){
return vector (a.x+b.x,a.y+b.y);
}
vector operator *(vector a,double t){
return vector (a.x*t,a.y*t);
}
bool operator <(vector a,vector b)
{
return a.x<b.x||a.x==b.x&&a.y<b.y;
}
int n,m;
int dcmp(double x)
{
if (fabs(x)<eps) return 0;
return x<0?-1:1;
}
double dot(vector a,vector b)
{
return a.x*b.x+a.y*b.y;
}
double cross(vector a,vector b)
{
return a.x*b.y-a.y*b.x;
}
double len(vector a)
{
return sqrt(a.x*a.x+a.y*a.y);
}
double distl(point a,point b,point c)
{
vector v=a-b; vector u=c-b;
return fabs(cross(u,v))/len(u);
}
void convexhull()
{
sort(a+1,a+n+1);
m=0;
if (n==1) {
ch[++m]=a[1];
return;
}
for (int i=1;i<=n;i++){
while (m>1&&cross(ch[m-1]-ch[m-2],a[i]-ch[m-2])<=0) m--;
ch[m++]=a[i];
}
int k=m;
for (int i=n-1;i>=1;i--){
while (m>k&&cross(ch[m-1]-ch[m-2],a[i]-ch[m-2])<=0) m--;
ch[m++]=a[i];
}
m--;
}
double rotating()
{
if (m<=2) return 0;
if (m==3) return fabs(cross(ch[1]-ch[0],ch[2]-ch[0]))/2;
double ans=0;
int i,j,k;
for (int i=0;i<m;i++){
j=(i+1)%m;
k=(j+1)%m;
//cout<<i<<" "<<j<<" "<<k<<endl;
while (fabs(cross(ch[i]-ch[j],ch[i]-ch[k]))<fabs(cross(ch[i]-ch[j],ch[i]-ch[(k+1)%m]))) k=(k+1)%m;
while (i!=j&&k!=i) {
ans=max(ans,fabs(cross(ch[i]-ch[j],ch[i]-ch[k])));
while (fabs(cross(ch[i]-ch[j],ch[i]-ch[k]))<fabs(cross(ch[i]-ch[j],ch[i]-ch[(k+1)%m]))) k=(k+1)%m;
j=(j+1)%m;
}
}
return ans/2.0;
}
int main()
{
freopen("a.in","r",stdin);
freopen("my.out","w",stdout);
while (scanf("%d",&n)!=EOF ) {
for (int i=1;i<=n;i++) scanf("%lf%lf",&a[i].x,&a[i].y);
convexhull();
double ans=rotating();
printf("%.2lf\n",ans);
}
}