Surround the Trees HDU - 1392 凸包周长(Andrew)

这题小trick在于n=2是不能乘2的,也就是只算两点距离

看你怎么理解凸包吧。。

//#include<bits/stdc++.h>  
//#pragma comment(linker, "/STACK:1024000000,1024000000")   
#include<stdio.h>  
#include<algorithm>  
#include<queue>  
#include<string.h>  
#include<iostream>  
#include<math.h>  
#include<set>  
#include<map>  
#include<vector>  
#include<iomanip>  
using namespace std;  
  
const double pi=acos(-1.0);  
#define ll long long  
#define pb push_back

#define sqr(a) ((a)*(a))
#define dis(a,b) sqrt(sqr(a.x-b.x)+sqr(a.y-b.y))

const double eps=1e-6;
const int maxn=1e3+56;
const int inf=0x3f3f3f3f;

int n;
int tot;	//凸包上点数

struct Point{
	double x,y;
	Point(){}
	Point(double x,double y):x(x),y(y){}
}point[maxn],vertex[maxn];

bool cmp(Point a,Point b){
	return(a.y<b.y||(a.y== b.y && a.x<b.x));
}

double xmult(Point p1,Point p2,Point p3){	//p3p1,p3p2的夹角测试
	return ( (p1.x-p3.x)*(p2.y-p3.y)-(p1.y-p3.y)*(p2.x-p3.x) );
}		//正表示p1在p2的顺时针方向

int Andrew(){		//返回凸包顶点数
	sort(point,point+n,cmp);
	int top=1;
	vertex[0]=point[0];vertex[1]=point[1];
	for(int i=2;i<n;i++){
		while(top && xmult(point[i],vertex[top],vertex[top-1])>eps)top--;
		vertex[++top]=point[i];
	}
	int len=top;
	vertex[++top]=point[n-2];
	for(int i=n-3;i>=0;i--){
		while(top!=len && xmult(point[i],vertex[top],vertex[top-1])>eps)top--;
		vertex[++top]=point[i];
	}
	return top;
}

int main(){
	while(~scanf("%d",&n) && n){
		for(int i=0;i<n;i++){
			scanf("%lf%lf",&point[i].x,&point[i].y);
		}
		if(n==1){
			puts("0.00");continue;
		}
		int tot=Andrew();
		double ans=0;
		for(int i=0;i<tot;i++){
			ans+=dis(vertex[i],vertex[(i+1)%tot]);
		}
		if(n==2)ans/=2.0;
		printf("%.2lf\n",ans);
	}
}


posted @ 2017-09-27 00:12  Drenight  阅读(130)  评论(0编辑  收藏  举报