POJ 2079

呃,不知道我用的算不算卡壳,总有点枚举的意思。

先求凸包,然后,枚举其中一点,再枚举另一点作为结尾,这个向量旋转一周后,求出最大值面积。这里面用的是旋转卡壳判断的那个式子。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;

struct point{
	int x,y;
}p[50050];
int n;

int ans[50050],st[50050],cnt,stop;

bool cmp(point A, point B){
	if(A.y<B.y) return true;
	else if(A.y==B.y){
		if(A.x<B.x) return true;
	}
	return false;
}

int multi(point a,point b,point c){
	point p1; p1.x=a.x-c.x; p1.y=a.y-c.y;
	point p2; p2.x=b.x-c.x; p2.y=b.y-c.y;
	return p1.x*p2.y-p1.y*p2.x;
}

void slove(){
	cnt=stop=0;
	st[stop++]=0; st[stop++]=1;
	for(int i=2;i<n;i++){
		while(stop>1&&multi(p[i],p[st[stop-1]],p[st[stop-2]])>0) stop--;
		st[stop++]=i;
	}
	for(int i=0;i<stop;i++)
	ans[cnt++]=st[i];
	stop=0; st[stop++]=n-1; st[stop++]=n-2;
	for(int i=n-3;i>=0;i--){
		while(stop>1&&multi(p[i],p[st[stop-1]],p[st[stop-2]])>0) stop--;
		st[stop++]=i;
	}
	for(int i=1;i<stop;i++)
	ans[cnt++]=st[i];
}

double Triangle(point a,point b,point c){
	point p1; p1.x=a.x-c.x; p1.y=a.y-c.y;
	point p2; p2.x=b.x-c.x; p2.y=b.y-c.y;
	return fabs((p1.x*p2.y-p1.y*p2.x)*1.0)/2.0;
}

double Area(){
	int q;
	double anst=0;
	for(int i=0;i<cnt;i++){
		q=i+1;
		for(int j=i+1;j<cnt;j++){
		while(Triangle(p[ans[i]],p[ans[j]],p[ans[q]])<Triangle(p[ans[i]],p[ans[j]],p[ans[(q+1)%cnt]]))
		q=(q+1)%cnt;
		anst=max(anst,Triangle(p[ans[i]],p[ans[j]],p[ans[q]]));
		}
	}
	return anst;
}

int main(){
	while(scanf("%d",&n)!=EOF){
		if(n==-1) break;
		for(int i=0;i<n;i++){
			scanf("%d%d",&p[i].x,&p[i].y);
		}
		sort(p,p+n,cmp);
		slove();
		double anst=0;
		anst=max(anst,Area());
		printf("%.2lf\n",anst);
	}
	return 0;
}

  

POJ 2079,布布扣,bubuko.com

时间: 2024-10-12 09:04:28

POJ 2079的相关文章

POJ 2079 凸包最大内接三角形

Triangle Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 8038   Accepted: 2375 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

poj 2079 Triangle,旋转卡壳求点集的最大三角形

给出一个点集,求顶点在点集中的最大的三角形面积. 我们知道这三角形的三个点肯定在凸包上,我们求出凸包之后不能枚举,因为题目n比较大,枚举的话要O(n^3)的数量级,所以采用旋转卡壳的做法: 首先枚举三角形的第一个顶点i, 初始化第二个顶点j=i+1和第三个顶点k=j+1,对k进行循环,直到找到第一个k使得cross(i,j,k)>cross(i,j,k+1),如果k==i进入下一次循环. 对j,k进行旋转,每次循环之前更新最大值,然后固定一个j,同样找到一个k使得cross(i,j,k)>cr

hdu 3934&amp;&amp;poj 2079 (凸包+旋转卡壳+求最大三角形面积)

链接:http://poj.org/problem?id=2079 Triangle Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 8173   Accepted: 2423 Description Given n distinct points on a plane, your task is to find the triangle that have the maximum area, whose vertices

poj 2079 Triangle 凸包+旋转卡壳

题意: 给平面上n个点,求这n个点组成的最大三角形面积. 分析: 旋转卡壳,但要注意和求平面最远点对的区别,最大三角形的边不一定在凸包上的,也贴出以前写的求平面最远点对的poj 2187代码作为对比. 代码: //poj 2079 //sep9 #include <iostream> #include <algorithm> using namespace std; const int maxN=50012; struct P { int x,y; }pnt[maxN],cnt[m

【旋转卡壳】POJ 2079 Triangle

通道:http://poj.org/problem?id=2079 题意:n个点选面积最大的三角形 代码: 1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 #include <cmath> 5 using namespace std; 6 7 const int MAX_N = 100007; 8 9 struct Point { 10 double x, y; 11 Po

●POJ 2079 Triangle

题链: http://poj.org/problem?id=2079 题解: 计算几何,凸包,旋转卡壳 复杂度O(N^2),(O(N)什么的就不说了,我觉得我看过的O(N)方法正确性都有问题,虽然有些AC了,那应该是鲁棒性太强了,谁叫他们非要每挪动一步都取MAX的呢) 做法: (三角形的三个顶点在凸包的顶点上,同时显然三角形的底边不一定为凸包的边啦!) 枚举i,j两点,使得有向线段$\vec{ij}$作为三角形底边. 然后在有向线段$\vec{ij}$的右侧区域(凸包上),寻找k点使得三角形ij

POJ 2079 Triangle [旋转卡壳]

Triangle Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 9525   Accepted: 2845 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

poj 2079 Triangle(旋转卡壳)

Triangle Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 8917   Accepted: 2650 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

【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