最大三角形(凸包)

B - 最大三角形
Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

老师在计算几何这门课上给Eddy布置了一道题目,题目是这样的:给定二维的平面上n个不同的点,要求在这些点里寻找三个点,使他们构成的三角形拥有的面积最大。 
Eddy对这道题目百思不得其解,想不通用什么方法来解决,因此他找到了聪明的你,请你帮他解决这个题目。
 

Input

输入数据包含多组测试用例,每个测试用例的第一行包含一个整数n,表示一共有n个互不相同的点,接下来的n行每行包含2个整数xi,yi,表示平面上第i个点的x与y坐标。你可以认为:3 <= n <= 50000 而且 -10000 <= xi, yi <= 10000.
 

Output

对于每一组测试数据,请输出构成的最大的三角形的面积,结果保留两位小数。 
每组输出占一行。 
 

Sample Input

     
     
3 3 4 2 6 3 7 6 2 6 3 9 2 0 8 0 6 6 7 7
 

Sample Output

     
     
1.50 27.00

#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;
#define esp 1e-6
#define PI acos(-1)
int Sign(double x){//判断x大于0,小于0,还是等于0 
	return fabs(x)<esp?0:x>0?1:-1;
}
struct Point{ //存点 
	double x,y;
	Point(){
	}
	Point(double xx,double yy):x(xx),y(yy) { }

	Point operator-(const Point & p) const {
		return Point(x-p.x,y-p.y);
	}

	bool operator <(const Point & p) const {
		if( y < p.y)
			return true;
		else if( y > p.y)
			return false;
		else
			return x < p.x;
	}
};

typedef Point Vector;
double Cross(const Vector & v1, const Vector & v2){//叉积
	return v1.x * v2.y - v2.x * v1.y;
}
double Distance(const Point & p1,const Point & p2){//求距离 
	return sqrt( (p1.x - p2.x)*(p1.x - p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
}

struct Comp { //用来定义极角排序规则的函数对象
	Point p0; //以p0为原点进行极角排序,极角相同的,离p0近算小
	Comp(const Point & p):p0(p.x,p.y) { }
	bool operator ()(const Point & p1,const Point & p2) const {
		int s = Sign( Cross(p1-p0,p2-p0));
		if( s > 0)
			return true;
		else if( s < 0)
			return false;
		else {
			if( Distance(p0,p1)<Distance(p0,p2))
				return true;
			else
				return false;
		}
	}
};

int Graham(vector<Point> & points,vector<Point> & stack) {//求凸包 
	//points是点集合
	if( points.size() < 3)
		return 0; //返回凸包顶点数
	stack.clear();
	//先按坐标排序,最左下的放到points[0]
	sort(points.begin(),points.end());
	//以points[0] 为原点进行极角排序
	sort(points.begin()+1,points.end(),Comp(points[0]));
	stack.push_back(points[0]);
	stack.push_back(points[1]);
	stack.push_back(points[2]);
	for(int i = 3; i< points.size(); ++i) {
		while(true) {
			Point p2 =* (stack.end()-1);
			Point p1 = *(stack.end()-2);
			if( Sign(Cross(p2-p1,points[i]-p2) <= 0))
				//p2->points[i]没有向左转,就让p2出栈
				stack.pop_back();
			else
				break;
		}
		stack.push_back(points[i]);
	}
	return stack.size();
}
int main(){
    double N,L,l,n1,xx1,yy1,xx2,yy2;
    int n;
    Point p;
    vector<Point> points;
	vector<Point> stack;
    while(~scanf("%d",&n)&&n!=0){
    	double res=0;
    	points.clear();
    	stack.clear();
    	for(int i=0;i<n;i++){
    		scanf("%lf %lf",&n1,&l);
    		p.x=n1,p.y=l;
        	points.push_back(p);
		}
		int size = Graham(points,stack);
		for(int i=0;i<=size-1;i++)
		for(int j=i+1;j<=size-1;j++)
		for(int k=j+1;k<=size-1;k++)
		{
			xx1=stack[j].x-stack[i].x;//求面积。任取三个点找最大的
			yy1=stack[j].y-stack[i].y;
			xx2=stack[k].x-stack[i].x;
			yy2=stack[k].y-stack[i].y;
			res=max(fabs(xx1*yy2-xx2*yy1),res);
		}
	     printf("%.2lf\n",fabs(res)/2);
	    }
       
    } 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值