题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392
题意:给你n棵树,让你用一根绳子把全部树围起来,要绳子尽可能的短,其实也就是求凸包的周长
解析:凸包裸题,然而我莫名其妙调了两个钟的bug,我还是太菜了
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <stack>
using namespace std;
const int maxn = 1000000+10;
const double eps = 1e-5;
struct point
{
double x;
double y;
point() {}
point(double _x,double _y)
{
x = _x;
y = _y;
}
bool operator < (const point b)const
{
if(y==b.y)
return x<b.x;
return y<b.y;
}
}a[maxn],ans[maxn];
double x_mul(point p0,point p1,point p2)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
double dis(point a,point b)
{
return sqrt(1.0*(a.x-b.x)*(a.x-b.x)+1.0*(a.y-b.y)*(a.y-b.y));
}
//极角排序
int cmp(point t1,point t2)
{
double tmp=x_mul(a[0],t1,t2);
if(tmp==0)
return dis(a[0],t1)<dis(a[0],t2);
return tmp>0;
}
double graham(int n)
{
sort(a,a+n);
sort(a+1,a+n,cmp);
ans[0] = a[0];
ans[1] = a[1];
ans[2] = a[2];
int top = 2;
for(int i=3;i<n;i++)
{
while(top>=2&&x_mul(ans[top-1],ans[top],a[i])<0)
top--;
ans[++top] = a[i];
}
double res = 0;
for(int i=0;i<top;i++)
{
//printf("%.2f %.2f\n",ans[i].x,ans[i].y);
res += dis(ans[i],ans[i+1]);
}
//printf("%.2f %.2f\n",ans[top].x,ans[top].y);
res += dis(ans[top],ans[0]);
return res;
}
int main(void)
{
int n;
while(~scanf("%d",&n)&&n)
{
for(int i=0;i<n;i++)
scanf("%lf %lf",&a[i].x,&a[i].y);
if(n==1)
{
puts("0.0");
continue;
}
else if(n==2)
{
printf("%.2f\n",dis(a[0],a[1]));
continue;
}
double ans = graham(n);
printf("%.2f\n",ans);
}
return 0;
}