题意: 把树围起来。
思路:简单凸包。
AC代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <map>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <set>
const int mod = 1e9+7;
#define ll long long
#define llu unsigned long long
using namespace std;
map<string,int>mp;
const int maxn = 100550+10;
const double eps = 0.0000001;
int v[maxn],a[maxn];
struct Point {
double x,y;
Point(double x=0,double y=0):x(x),y(y){}
};
int n;
Point tree[150],s[150];
double cross(Point a,Point b,Point c) {
return ((b.x-a.x)*(c.y-a.y) - (b.y-a.y)*(c.x-a.x));
}
double dis(Point a,Point b) {
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool cmp(Point a,Point b) {
if(a.x != b.x) return a.x<b.x;
else return a.y<b.y;
}
bool cmp1(Point a,Point b) {
double m = cross(tree[0],a,b);
if(fabs(m-0) < eps) {
if(dis(tree[0],b) > dis(tree[0],a)) return true;
else return false;
}
else {
if(m>0) return true;
else return false;
}
}
int main(){
while(scanf("%d",&n)!=EOF){
if(n==0) break;
for(int i=0;i<n;i++) {
scanf("%lf%lf",&tree[i].x,&tree[i].y);
}
if(n==1) {
printf("0.00\n"); continue;
}
if(n==2) {
printf("%.2lf\n",dis(tree[0],tree[1])); continue;
}
sort(tree,tree+n,cmp);
sort(tree+1,tree+n,cmp1);
s[0].x = tree[0].x;
s[0].y = tree[0].y;
s[1].x = tree[1].x;
s[1].y = tree[1].y;
int top = 1;
for(int i=2;i<n;i++) {
while(top>=1 && cross(s[top-1],s[top],tree[i]) < 0) top--;
s[++top] = tree[i];
}
/* cout<<top<<endl;
for(int i=0;i<=top;i++) {
cout<<s[i].x<<" "<<s[i].y<<endl;
}cout<<endl;*/
double sum = 0;
for(int i=0;i<top;i++) {
// cout<<dis(s[i],s[i+1])<<endl;
sum += dis(s[i],s[i+1]);
}
sum += dis(s[0],s[top]);
printf("%.2lf\n",sum);
}
return 0;
}