swust 最小凸包的面积

http://acm.swust.edu.cn/problem/0249/


求凸包的最小的面积有一个公式:




知道公式,直接裸过;


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;

const int maxn = 1000 + 10;
#define INF 0x3f3f3f3f
#define eps 10e-8
//const int maxn = 1000 + 10;
int n,m,k;
struct Node
{
	double x,y;
	double angles;
}a[maxn];

double distances(Node x,Node y)
{
	return sqrt((x.x - y.x) * (x.x - y.x) + (x.y - y.y) * (x.y - y.y));
	
}
double angle(Node s,Node x)
{
	return acos((x.x - s.x) / sqrt((x.x - s.x) * (x.x - s.x) + (x.y - s.y) * (x.y - s.y)));
}
Node s;
bool cmp(Node x,Node y)
{
	if(fabs(x.angles - y.angles) < eps)
	return fabs(x.x - s.x) < fabs(y.x - s.x);
	return x.angles < y.angles;
}
double across_multi(Node p1,Node p2,Node p3)
{
	return (p1.x - p2.x)* (p3.y - p2.y) - (p1.y - p2.y) *(p3.x - p2.x);
}
double gramham()
{
	s = a[1];
	for(int i = 2; i <= n; i ++)
	{
		if(a[i].y < s.y || (a[i].y == s.y && a[i].x < s.x))
		s = a[i];
	}
	for(int i = 1; i <= n; i ++)
	{
		if(a[i].x == s.x && a[i].y == s.y)
		{
			a[i].angles  = s.angles = 0;
		}
		else 
		{
			a[i].angles = angle(s,a[i]);
		}
	}
	sort(a+1,a+n+1,cmp);
//	for(int i = 1; i <= n; i ++)
//	cout << a[i].x << " JJJ " << a[i].y << endl;
	Node st[maxn];
	if(n == 1)
	return 0;
	if(n == 2)
	return 0;
	int len = 1;
	st[len ++] = a[1];
	st[len ++] = a[2];
	st[len ++] = a[3];
	for(int i = 4; i <= n; i ++)
	{
		while(across_multi(st[len -1],st[len  -2],a[i]) <= 0)
		len --;
		st[len ++] = a[i];
 	}
// 	for(int i = 1; i < len; i ++)
// 	cout << a[i].x << " " << a[i].y << endl;
 	double ans = 0;
 	for(int i = 1; i < len - 1; i ++)
 	{
 		ans += (st[i].x * st[i + 1].y - st[i + 1].x * st[i].y) / 2;
	}
	return ans;
}
int main()
{
	int Tcase;
	scanf("%d",&Tcase);
	while( Tcase --)
	{
		scanf("%d",&n);
		for(int i = 1; i <= n; i ++)
		scanf("%lf%lf",&a[i].x,&a[i].y);
		double ans = gramham();
		printf("%.1f\n",ans);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值