poj 2079 Triangle (旋转卡壳)

Triangle
Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 9495 Accepted: 2835

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

[Submit]   [Go Back]   [Status]   [Discuss]


题目大意:求以给出的点为顶点的三角形的最大面积。

题解:旋转卡壳

固定i,j两个顶点后,k的位置是单调的

#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 (true) {
		scanf("%d",&n);
		if (n==-1) break;
		for (int i=1;i<=n;i++) scanf("%lf%lf",&a[i].x,&a[i].y);
		convexhull();
		double ans=rotating();
		printf("%.2lf\n",ans);
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值