1. 程式人生 > >USACO FC,二維凸包

USACO FC,二維凸包

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
const int maxn=10000+10;
using namespace std;
struct node{double x,y;};
node p[maxn],z[maxn];
bool cmp(node u,node v){return u.x<v.x;}
bool cmpxl(node a,node b,node c){
	return (a.y-b.y)*(b.x-c.x)<(b.y-c.y)*(a.x-b.x);
}
double dist(node a,node b){
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main(){
	freopen("fc.in","r",stdin);
	freopen("fc.out","w",stdout);
	int i,j,k,m,n;
	cin>>n;
	for(i=1;i<=n;i++)scanf("%lf%lf",&p[i].x,&p[i].y);
	sort(p+1,p+n+1,cmp);
	z[1]=p[1];z[2]=p[2];
	int top=2;
	for(i=3;i<=n;i++){
		while(top>1 && cmpxl(p[i],z[top],z[top-1]))top--;
		z[++top]=p[i];
	}
	double ans=0;
	for(i=1;i<top;i++)	ans+=dist(z[i],z[i+1]);
	
	z[1]=p[n];z[2]=p[n-1];	
	top=2;
	for(i=n-2;i>=1;i--){
		while(top>1 && cmpxl(p[i],z[top],z[top-1]))top--;
		z[++top]=p[i];
	}	
	for(i=1;i<top;i++)	ans+=dist(z[i],z[i+1]);
	printf("%.2lf\n",ans);
	return 0;
}