Surround the Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11188 Accepted Submission(s): 4347
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.

There are no more than 100 trees.
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.
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<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
#define ll long long
#define ms(a,b)memset(a,b,sizeof(a))
#define eps 1e-10
#define inf 1e8
double a[4][4] = { {0,0,0,0},{0,-1,0,0},{0,0,0,-1},{0,-1,0,-1} } ;
int n;
double add(double a,double b)
{
if(abs(a+b)<eps*(abs(a)+abs(b))) return 0;
return a+b;
}
struct P
{
double x,y;
int number;
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));
}
P operator *(double d)
{
return P(x*d,y*d);
}
double dot (P p)
{
return add(x*p.x,y*p.y);
}
double det(P p)
{
return add(x*p.y,-y*p.x);
}
}ps[2000];
bool on_seg(P p1,P p2,P q)
{
return (p1-q).det(p2-q)==0&&(p1-q).dot(p2-q)<=0;
}
P intersection(P p1,P p2,P q1,P q2)
{
return p1+(p2-p1)*((q2-q1).det(q1-p1)/(q2-q1).det(p2-p1));
}
bool cmp_x(const P& p,const P & q)
{
if(p.x!=q.x) return p.x<q.x;
return p.y<q.y;
}
vector<P> convex_hull(P *pos,int n)
{
sort(ps,ps+n,cmp_x);
int k=0;
vector<P> qs(n*2);
for(int i=0;i<n;i++)
{
while(k>1&&(qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<=0) k--;
qs[k++]=ps[i];
}
for(int i=n-2,t=k;i>=0;i--)
{
while(k>t&&(qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<=0) k--;
qs[k++]=ps[i];
}
qs.resize(k-1);
return qs;
}
double dist(P p,P q)
{
return sqrt((p-q).dot(p-q));
}
int main()
{
int n;
while(~scanf("%d",&n))
{
if(n==0) break;
for(int i=0;i<n;i++)
scanf("%lf%lf",&ps[i].x,&ps[i].y);
double res=0.0;
if(n==2)
{
printf("%.2lf\n",dist(ps[0],ps[1]));
continue;
}
vector<P> qs=convex_hull(ps,n);
int qs_size=qs.size();
for(int i=0;i<qs.size();i++)
{
res+=dist(qs[i],qs[(i+1)%qs_size]);
}
printf("%.2lf\n",res);
}
return 0 ;
}

本文介绍了一种计算平面上一系列点形成的凸包周长的方法。通过寻找这些点构成的最外层边界,即凸包,并计算这个凸包的总长度。此算法适用于需要估算包围多个目标最小边界的应用场景。

被折叠的 条评论
为什么被折叠?



