Surround the Trees(凸包)


A - Surround the Trees
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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<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;
    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);//顶点个数
		if(size>=1)
		{
		for(int i=0;i<size-1;i++)//求凸包周长 
        res+=Distance(stack[i],stack[i+1]);
        res+=Distance(stack[0],stack[size-1]);
        res+=2*PI*L;
        printf("%.2lf\n",res);
		}
		else
		{
		if(points.size()==1)//只有一个点
		printf("0.00\n");
		else if(points.size()==2)//只有两个点
		{
		res=Distance(points[0],points[1]);
		printf("%.2lf\n",res); 
		} 
		}   
    }    
    return 0;
} 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值