HDU - 1392 Surround the Trees

比较简单的题,凸包模板A了
#include <iostream>
#include <cstring>
#include<algorithm>
#include<vector>
#include<stack>
#include<cstdio>
#include<cstdlib>
#include<cmath>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 300;
const double PI = acos(-1.0);
double EPS = 1e-10;
double add(double a, double b)
{
	if(abs(a + b)< EPS * (abs(a) + abs(b))) return 0;
	return a + b;
}
int sgn(double x)
{
	if(fabs(x) < EPS) return 0;
	if(x<0)return -1;
	else return 1;
}
struct P
{
	double x, y;
	P(){}
	P(double x,double y):x(x),y(y){}
	P operator + (P p){
		return P(add(x, p.x),add(y,p.y));
	}
	P operator - (P p){
		return P(add(x, -p.x),add(y,-p.y));
	}
	double operator ^(P &b){
		return x*b.y - y* b.x;
	}
	double operator *(P &b){
		return x*b.x + y * b.y;
	}
	void transXY(double B)
	{
		double tx = x,ty = y;
		x = tx*cos(B) - ty*sin(B);
		y = tx*sin(B) - tx*cos(B);
	}
};
double dist( P a, P b)
{
	return sqrt((a-b)*(a-b));
}
P list[MAXN];
int Stack[MAXN],top;
bool _cmp(P p1, P p2)
{
	double tmp = (p1 - list[0])^(p2 - list[0]);
	if(sgn(tmp) > 0) return true;
	else if(sgn(tmp) == 0 && sgn(dist(p1,list[0]) - dist(p2,list[0])) <= 0) return true;
	else return false;
}
void graham(int n)
{
	P p0;
	int k = 0;
	p0 = list[0];
	for(int i = 1; i < n; i++)
	{
		if((p0.y>list[i].y) || (p0.y == list[i].y && p0.x > list[i].x))
		{
			p0 = list[i];
			k = i;
		}
	}
	swap(list[k], list[0]);
	sort(list+1,list + n, _cmp);
	if( n == 1)
	{
		top = 1;
		Stack[0] = 0;
		return;
	}
	if(n == 2)
	{
		top = 2;
		Stack[0] = 0;
		Stack[1] = 1;
	}
	Stack[0] = 0;
	Stack[1] = 1;
	top = 2;
	for(int i = 2; i < n; i++)
	{
		while(top>1 && sgn(( list[Stack[top -1]] - list[Stack[top - 2]]) ^ (list[i] - list[Stack[top-2]]))<=0)
			top--;
		Stack[top++] = i;
	}
}
int main()
{
#ifdef LOCAL
	freopen("dd.txt","r",stdin);
#endif
	int N;
	while(~scanf("%d", &N)&&N)
	{
		for(int i = 0; i < N; i++)
		{
			scanf("%lf%lf", &list[i].x, &list[i].y);
		}
		graham(N);
		double res = 0;
		int  zx = 0, zy = 0;
		for(int i = 0; i < top - 1; i++)
		{
			res += dist(list[Stack[i]],list[Stack[i+1]]);
		}
		if(top>=3)//如果大于等于三个凸点,证明不是一条线,需要头尾相连。
		{
			res += dist(list[Stack[0]],list[Stack[top-1]]);
		}
		printf("%.2lf\n", res); 
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值