hdu 最大三角形(凸包+旋转卡壳)

时间:2023-02-02 12:34:19
老师在计算几何这门课上给Eddy布置了一道题目,题目是这样的:给定二维的平面上n个不同的点,要求在这些点里寻找三个点,使他们构成的三角形拥有的面积最大。
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

旋转卡壳:http://www.cnblogs.com/Booble/archive/2011/04/03/2004865.html

代码:

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define eps 1e-8
struct node
{
int x,y;
}
;
node p[];
node res[];
int cross(node p0,node p1,node p2)
{
return (p0.x-p2.x)*(p1.y-p2.y)-(p1.x-p2.x)*(p0.y-p2.y);
}
bool cmp(node a,node b)
{
if(a.x==b.x)
return a.y<b.y;
else
return a.x<b.x;
}
int Graham(int n)
{
int len;
int top=;
sort(p,p+n,cmp);
for(int i=; i<n; i++)
{
while(top>&&cross(res[top-],p[i],res[top-])<=)
top--;
res[top++]=p[i];
}
len=top;
for(int i=n-; i>=; i--)
{
while(top>len&&cross(res[top-],p[i],res[top-])<=)
top--;
res[top++]=p[i];
}
if(n>)
top--;
return top;
}
int main()
{
int n;
while(cin>>n)
{
for(int i=; i<n; i++)
cin>>p[i].x>>p[i].y;
int dian=Graham(n);
int ans=-;
for(int i=; i<dian; i++)
for(int j=i+; j<dian; j++)
for(int k=j+; k<dian; k++)
ans=max(ans,cross(res[j],res[k],res[i]));
printf("%.2lf\n",0.5*ans);
}
return ;
}