Surround the Trees&&凸包入门题


Problem 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.

Surround the Trees&&凸包入门题_第1张图片

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
AC代码:
#include<iostream>
#include<string.h>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#define N 105
using namespace std;
struct Point
{
	int  x;
	int  y;
}p[N];
int s[N],n,top;
int cross(const Point& a,const Point& b,const Point& c)
{return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);}
double dis(const Point& a,const Point& b)
{return sqrt((double)(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}
bool cmp(const Point& a,const Point& b)
{
	int res=cross(p[0],a,b);
	if(res>0||(res==0&&dis(p[0],a)<dis(p[0],b)))return true;
	return false;
}
void graham()
{
	s[0]=0;
	s[1]=1;
	top=1;
	for(int i=2;i!=n;++i)
	{
		while(top&&cross(p[s[top-1]],p[s[top]],p[i])<0) top--;
		 s[++top]=i;
	}
}
int main()
{
	while(~scanf("%d",&n),n)
	{
		for(int i=0;i!=n;++i)
			cin>>p[i].x>>p[i].y;
		cout.setf(ios::fixed);//设置cout为定点输出格式		cout.precision(2);//保留两位有效数字
		if(n==1) {
			cout<<"0.00"<<endl;
			continue;
		}
		else if(n==2) {
			cout<<dis(p[0],p[1])<<endl;
			continue;
		}
		Point ans=p[0];
		int u=0;
		for(int i=1;i!=n;++i)
			if(ans.y>p[i].y||(ans.y==p[i].y&&ans.x>p[i].x)) ans=p[i],u=i;
		  if(u) swap(p[u],p[0]);
		  sort(p+1,p+n,cmp);
		  graham();
		  double res=dis(p[s[0]],p[s[top]]);
		  for(int i=0;i!=top;++i)
			  res+=dis(p[s[i]],p[s[i+1]]);
		  cout<<res<<endl;
		 }return 0;
}

你可能感兴趣的:(Integer,less,input,each,output,pair)