您的位置:首页 > 其它

HDU 1392 Surround the Trees(凸包)

2016-05-08 20:20 447 查看
【题意】给n个点求凸包。

【分析】求凸模板,这里注意特判n==2的情况,没注意到WA一回!

【AC代码】

#include <set>
#include <map>
#include <math.h>
#include <vector>
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 10010;
const double PI = acos(-1);
struct Point{
double x,y;
Point(){}
Point(int x,int y):x(x),y(y){}
bool operator<(const Point &rhs)const{
if(x==rhs.x) return y<rhs.y;
return x<rhs.x;
}
}q[maxn],p[maxn];
double Cross(const Point &a,const Point &b){
return a.x*b.y-a.y*b.x;
}
double get_Dis(const Point &a,const Point &b){
return (double)sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main(){
int n;
while(scanf("%d",&n)!=EOF){
if(n==0) return 0;
for(int i=0; i<n; i++) scanf("%lf%lf",&q[i].x,&q[i].y);
sort(q,q+n);
if(n==1)
{
printf("0.00\n");
continue;
}
if(n==2)
{
printf("%.2lf\n",get_Dis(q[0],q[1]));
continue;
}
int m = 0;
for(int i=0; i<n; i++){//"下凸包"
while(m>1&&Cross(Point(p[m-1].x-p[m-2].x,p[m-1].y-p[m-2].y),Point(q[i].x-p[m-2].x,q[i].y-p[m-2].y))<=0) m--;
p[m++] = q[i];
}
int k = m;
for(int i=n-2; i>=0; i--){//"上凸包"
while(m>k&&Cross(Point(p[m-1].x-p[m-2].x,p[m-1].y-p[m-2].y),Point(q[i].x-p[m-2].x,q[i].y-p[m-2].y))<=0) m--;
p[m++] = q[i];
}
if(n>1) m--;
double sum = 0;
for(int i=0,j=1; i<m; i++,j++){
sum += get_Dis(p[i],p[j]);
}
printf("%.2f\n",sum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: