Surround the Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10345 Accepted Submission(s): 4009
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.
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
Sample Output
243.06
Source
题意:n个点 求凸包的周长
题解:凸包模板题
/******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
#include<bits/stdc++.h>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
#define A first
#define B second
const int mod=;
const int MOD1=;
const int MOD2=;
const double EPS=0.00000001;
typedef __int64 ll;
const ll MOD=;
const int INF=;
const ll MAX=1ll<<;
const double eps=1e-;
const double inf=~0u>>;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
struct point
{
double x,y;
point(double x=,double y=):x(x),y(y) {}
};
typedef point vec;
vec operator -(point a,point b)
{
return vec(a.x-b.x,a.y-b.y);
}
vec operator +(point a,point b)
{
return vec(a.x+b.x,a.y+b.y);
}
vec operator *(point a,double t)
{
return vec(a.x*t,a.y*t);
}
vec operator /(point a,double t)
{
return vec(a.x/t,a.y/t);
}
int dcmp(double x)
{
if(fabs(x)<=eps) return ;
return x<?-:;
}
double cross(vec a,vec b) ///叉积
{
return a.x*b.y-a.y*b.x;
}
bool cmp(point a,point b)
{
if(fabs(a.x-b.x)<=eps) return a.y<b.y;
return a.x<b.x;
}
double disn(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
/*两点之间的距离*/
}
void convexhull(point *s,int &n)
{
sort(s,s+n,cmp);
int m=;
point p[];
for(int i=; i<n; i++)
{
while(m> && dcmp(cross(p[m-]-p[m-],s[i]-p[m-]))<=)
m--;
p[m++]=s[i];
}
int k=m;
for(int i=n-; i>=; i--)
{
while(m>k && dcmp(cross(p[m-]-p[m-],s[i]-p[m-]))<=)
m--;
p[m++]=s[i];
}
m--;
n=m;
for(int i=; i<n; i++) s[i]=p[i];
/*建立凸包*/
}
int jishu;
int main()
{
while(scanf("%d",&jishu)!=EOF)
{
if(jishu==)
break;
point s[];
for(int i=; i<jishu; i++)
{
scanf("%lf %lf",&s[i].x,&s[i].y); }
if(jishu==)
{
printf("0.00\n");
continue;
}
if(jishu==)
{
printf("%.2f\n",disn(s[],s[]));
continue;
}
convexhull(s,jishu);
double sum=;
for(int i=; i<jishu-; i++)
{
sum+=disn(s[i],s[i+]);
}
sum+=disn(s[jishu-],s[]);
printf("%.2f\n",sum);
}
return ;
}