HDU1392--Surround the Trees--凸包

Description

There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So at first he must know the minimal required length of the rope. However, he does not know how to calculate it. Can you help him?  
The diameter and length of the trees are omitted, which means a tree can be seen as a point. The thickness of the rope is also omitted which means a rope can be seen as a line.



There are no more than 100 trees.
 

Input

The input contains one or more data sets. At first line of each input data set is number of trees in this data set, it is followed by series of coordinates of the trees. Each coordinate is a positive integer pair, and each integer is less than 32767. Each pair is separated by blank.

Zero at line for number of trees terminates the input for your program.
 

Output

The minimal length of the rope. The precision should be 10^-2.
 

Sample Input

    
    
9 12 7 24 9 30 5 41 9 80 7 50 87 22 9 45 1 50 7 0
 

Sample Output

    
    
243.06
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iomanip>
using namespace std;
struct Point
{
	int x,y;
}point[108],res[108];
bool cmp(Point a,Point b)
{
	if(a.x>b.x)return 0;
	else if(a.x==b.x)return a.y>b.y?0:1;
	return 1;
}
bool ral(Point p1,Point p2,Point p3)
{
	return ((p3.y-p1.y)*(p2.x-p1.x)>(p2.y-p1.y)*(p3.x-p1.x));
}
double distant(Point a,Point b)
{
	return sqrt(double((b.y-a.y)*(b.y-a.y)+(b.x-a.x)*(b.x-a.x)));
}
int main()
{
	int n;
	while(cin>>n&&n)
	{
		for(int i=0;i<n;i++)
		{
			cin>>point[i].x>>point[i].y;
		}
		if(n==1)
		{
			cout<<0.00<<endl;
			continue;
		}
		if(n==2)
		{
			double len1=sqrt(double(point[1].y-point[0].y)*(point[1].y-point[0].y)+(point[1].x-point[0].x)*(point[1].x-point[0].x));
			cout<<setiosflags(ios::fixed)<<setprecision(2)<<len1<<endl;
			continue;
		}
		sort(point,point+n,cmp);
		int m=-1;
		for(int i=0;i<n;i++)
		{
			while(m>=1&&!ral(res[m-1],res[m],point[i]))
			{
				m--;
			}
			res[++m]=point[i];
		}
		int k=m;
		for(int i=n-2;i>=0;i--)
		{
			while(m>k&&!ral(res[m-1],res[m],point[i]))
			{
				m--;
			}
			res[++m]=point[i];
		}
		double len=0;
		for(int i=0;i<m;i++)
		{
			len+=distant(res[i],res[i+1]);
		}
		cout<<setiosflags(ios::fixed)<<setprecision(2)<<len<<endl;
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值