题目链接:传送门
Triangle
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 9731 | Accepted: 2914 |
Description
Given n distinct points on a plane, your task is to find the triangle that have the maximum area, whose vertices are from the given points.
Input
The input consists of several test cases. The first line of each test case contains an integer n, indicating the number of points on the plane. Each of the following n lines contains two integer xi and yi, indicating the ith points. The last line of the input is an integer −1, indicating the end of input, which should not be processed. You may assume that 1 <= n <= 50000 and −10
4 <= xi, yi <= 10
4 for all i = 1 . . . n.
Output
For each test case, print a line containing the maximum area, which contains two digits after the decimal point. You may assume that there is always an answer which is greater than zero.
Sample Input
3 3 4 2 6 2 7 5 2 6 3 9 2 0 8 0 6 5 -1
Sample Output
0.50 27.00
解题思路:旋转卡壳求平面点集最大的三角形面积
貌似网上很多的代码都有问题
贴一组poj discuss里的测试数据
6 4 -5 6 -2 5 2 -4 5 -6 2 -5 -2 -1 显然(4,-5),(5,2),(-6,2)构成了面积 38.50 的三角形. 5 8 -3 8 -2 -2 6 -2 3 4 -2 -1 显然(4,-2),(8,-2),(-2,6) 面积 16 7 -3 8 -4 -10 -8 -6 -9 0 -6 -10 -9 3 0 -4 -1 由叉积(-3,8),(-4,-10),(-9,3) 面积 51.50
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 50009;
const int M = 1000;
const int INF = 0x3fffffff;
const double eps = 1e-8;
const double PI = acos(-1.0);
int sgn(double x)
{
if(fabs(x)<eps) return 0;
if(x<0) return -1;
else return 1;
}
struct Point
{
double x,y;
Point(){}
Point(double _x,double _y){
x = _x; y = _y;
}
Point operator - (const Point&b)const{
return Point(x-b.x,y-b.y);
}
//叉积
double operator ^ (const Point&b)const{
return x*b.y - y*b.x;
}
//点积
double operator * (const Point&b)const{
return x*b.x + y*b.y;
}
void input(){
scanf("%lf%lf",&x,&y);
}
};
struct Line
{
Point s,e;
Line(){}
Line(Point _s,Point _e){
s = _s; e = _e;
}
};
//两个点距离的平方
int dist(Point a,Point b)
{
return (a-b)*(a-b);
}
//求凸包,Graham算法
//先找出最左下角的点p,将其他点按照与p点的极角从小到大排序,极角相同,按到p点的距离从小到大排序
//用一个栈存储凸包上的点,如果点的个数n>3,Stack[0]=c1,Stack[1]=c2(点的编号)
//循环找点q使Stack[top-1],Stack[top-2]能够"左转",即(Stack[top-1]-Stack[top-2])^(q-Stack[i-1])<=0
//点的编号0~n-1
Point List[N];
int Stack[N],top;
//相对于list[0]的极角排序
bool _cmp(Point p1,Point p2)
{
double tmp = (p1-List[0])^(p2-List[0]);
if(sgn(tmp)>0) return true;
else if(sgn(tmp)==0 && sgn(dist(p1,List[0])-dist(p2,List[0])) <= 0)
return true;
else return false;
}
void Graham(int n)
{
Point p0;
int k = 0;
p0 = List[0];
//找最下边的一个点
for( int i = 1 ; i < n ; ++i ){
if( (p0.y > List[i].y) || (p0.y == List[i].y && p0.x > List[i].x) ){
p0 = List[i];
k = i;
}
}
swap(List[k],List[0]);
sort(List+1,List+n,_cmp);
if(n == 1){
top = 1;
Stack[0] = 0;
return;
}
if(n == 2){
top = 2;
Stack[0] = 0;
Stack[1] = 1;
return;
}
Stack[0] = 0;
Stack[1] = 1;
top = 2;
for( int i = 2 ; i < n ; ++i ){
while(top > 1 &&
sgn((List[Stack[top-1]]-List[Stack[top-2]])^(List[i]-List[Stack[top-2]])) <= 0)
top--;
Stack[top++] = i;
}
}
//旋转卡壳,求两点间距离的平方的最大值
//int rotating_calipers(Point p[],int n)
//{
// int ans = 0;
// Point v;
// int cur = 1;
// for( int i = 0 ; i < n ; ++i ){
// v = p[i]-p[(i+1)%n];
// while((v^(p[(cur+1)%n]-p[cur]))<0)
// cur = (cur+1)%n;
// ans = max(ans,max(dist(p[i],p[cur]),dist(p[(i+1)%n],p[(cur+1)%n])));
// }
// return ans;
//}
//旋转卡壳,求平面点集最大的三角形面积
double rotating_calipers(Point p[],int n)
{
double ans = 0;
int i,j,k;
for( i = 0 ; i < n ; ++i ){
for( j = (i+1)%n,k = (j+1)%n ; (j+1)%n != i ; j = (j+1)%n ){
while( sgn((p[(k+1)%n]-p[k])^(p[j]-p[i])) < 0 )
k = (k+1)%n;
ans = max(ans,fabs((p[k]-p[j])^(p[j]-p[i])));
}
}
return ans;
}
Point point[N];
int main()
{
int n;
while( ~scanf("%d",&n)&&n != -1 ){
for( int i = 0 ; i < n ; ++i ) List[i].input();
Graham(n);
for( int i = 0 ; i < top ; ++i ) point[i] = List[Stack[i]];
printf("%.2lf\n",rotating_calipers(point,top)/2);
}
return 0;
}