[POJ2079]Triangle 凸包+旋转卡壳 计算几何

Triangle
Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 10242 Accepted: 3074

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

Shanghai 2004 Preliminary

大致题意:

给你一堆点,让你取这些点中的三个点使这三个点组成的三角形面积最大,求最大面积。

题解:

首先我们能一眼看出,所求的点一定是凸包上的点。我们有一种很暴力的做法就是枚举i,j,k这三个点,时间复杂度为O(n^3),显然再怎么卡也是能把你卡掉的。这时候我们考虑旋转卡壳。我们发现找到i,j后,i,j在旋转的同时,k也满足旋转卡壳的旋转。于是我们可以枚举一个x,表示以i和i+x为底边。然后套用旋转卡壳模板即可。

AC代码:

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iostream>
using namespace std;
const int Maxn=50005;
struct node{
	int x,y;
}a[Maxn],ans[Maxn];
int n,k;
node operator -(const node &a,const node &b){
	node c;
	c.x=a.x-b.x;c.y=a.y-b.y;
	return c;
}
inline int cro(node a,node b){
	return a.x*b.y-b.x*a.y; 
}
bool cmp(node x,node y){
	if(x.y!=y.y)return x.y<y.y;
	return x.x<y.x;
}
void convex(){
	k=0;
	sort(a+1,a+1+n,cmp);
	for(int i=1;i<=n;i++){
		while(k>=2&&cro((a[i]-ans[k-1]),(ans[k]-ans[k-1]))>=0)k--;
		ans[++k]=a[i];
	}
	int lim=k;
	for(int i=n;i>=1;i--){
		while(k>=lim+1&&cro((a[i]-ans[k-1]),(ans[k]-ans[k-1]))>=0)k--;
		ans[++k]=a[i];
	}
	if(n>1)k--;
}
inline int add(int x,int y){
	x+=y;
	x%=k+1;
	if(x==0)x=1;
	return x;
}
inline int mx(int x,int y){
	return x>y?x:y;
}
/*inline int step(int x){
	x++;
	if(x>k)x=1;
	return x;
} */
inline int ab(int x){
	return x>0?x:-x;
}/*
void work(){//一种能快不少的奇技淫巧,然而我不会QAQ 
	int p=2,q=3,ansn=0;
	for(int i=1;i<=n;i++){
		scanf("%d%d",&a[i].x,&a[i].y);
	}
	convex();
	for(int i=1;i<=k;i++){
		while(cro((ans[p]-ans[i]),(ans[step(q)]-ans[i]))>cro((ans[p]-ans[i]),(ans[q]-ans[i])))q=step(q);
		ansn=mx(ansn,cro((ans[p]-ans[i]),(ans[q]-ans[i])));
		while(cro((ans[step(p)]-ans[i]),(ans[q]-ans[i]))>cro((ans[p]-ans[i]),(ans[q]-ans[i])))p=step(p);
		ansn=mx(ansn,cro((ans[p]-ans[i]),(ans[q]-ans[i])));
	}
	printf("%.2lf\n",double(ansn/2.0));
}*/
void work(){
	int q=2,ansn=0;
	for(int i=1;i<=n;i++){
		scanf("%d%d",&a[i].x,&a[i].y);
	}
	convex();
	for(int j=1;j<=k/2+1;j++){
		for(int i=1;i<=k;i++){
			while(cro((ans[add(i,j)]-ans[i]),(ans[add(q,1)]-ans[i]))>cro((ans[add(i,j)]-ans[i]),(ans[q]-ans[i])))q=add(q,1);
			ansn=mx(ansn,cro((ans[add(i,j)]-ans[i]),(ans[q]-ans[i])));
		}
	}
	printf("%.2lf\n",double(ansn/2.0));
}
int main(){
	while(1){
		scanf("%d",&n);
		if(n==-1)break;
		work();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值