【POJ 2079】Triangle

Triangle
Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 8437 Accepted: 2497

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

Source


旋转卡壳求凸包上的最大三角形面积。


只要把旋转卡壳变形一下:

之前的旋转卡壳是求一个点及其相邻点的对踵点,现在求一个点的时候枚举其他每一个点和这个点的对踵点即可。


#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define eps 1e-9
using namespace std;
struct Point
{
	double x,y;
}a[50005];
int n,tail,h[50005];
bool cmp(Point a,Point b)
{
	if (a.x==b.x) return a.y<b.y;
	return a.x<b.x;
}
double Cross(Point a,Point b,Point c)
{
	return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
void Graham()
{
	sort(a+1,a+1+n,cmp);
	tail=0;
	for (int i=1;i<=n;i++)
	{
		while (tail>=2&&Cross(a[h[tail-2]],a[h[tail-1]],a[i])<eps)
			tail--;
		h[tail++]=i;
	}
	int tmp=tail;
	h[tail++]=n-1;
	for (int i=n-2;i;i--)
	{
		while (tail>tmp&&Cross(a[h[tail-2]],a[h[tail-1]],a[i])<eps)
			tail--;
		h[tail++]=i;
	}
	tail--;
}
double Rotating_calipers()
{
	double ans=0.0;
	for (int i=0;i<tail;i++)
	{
		int x=i+1;
		for (int j=i+1;j<=tail;j++)
		{
			while (fabs(Cross(a[h[i]],a[h[j]],a[h[x+1]]))>fabs(Cross(a[h[i]],a[h[j]],a[h[x]])))
				x=(x+1)%tail;
			ans=max(ans,fabs(Cross(a[h[i]],a[h[j]],a[h[x]])));
		}
	}
	return ans/2.000;
}
int main()
{
        while (scanf("%d",&n)!=EOF)
	{
		if (n==-1) break;
		for (int i=1;i<=n;i++)
			scanf("%lf%lf",&a[i].x,&a[i].y);
		Graham();
		printf("%.2lf\n",Rotating_calipers());
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值