[日常摸鱼]HDU1348Wall-凸包

我学习进度慢得连我自己都怕…

 

题意:大概给$n$个点搞出它的凸包,然后还要在凸包外弄一层厚为$l$的东西,求这个东西的周长

 

我个滞涨居然把pi开成了int…搞了一个晚上才看见

凸包直接求,因为是凸多边形所以答案就是凸包的周长加上$2 \pi l$

 

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=1005;
const double pi=acos(-1);
struct Point
{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y){}
}p[N];
int T,n,L;
int q[N];
inline Point operator - (Point a,Point b)
{
    return Point(a.x-b.x,a.y-b.y);
}
inline double Cross(Point a,Point b)
{
    return a.x*b.y-a.y*b.x;
}
inline double sqr2(double x){return x*x;}
inline double lenght(Point a,Point b)
{
    return sqrt(sqr2(a.x-b.x)+sqr2(a.y-b.y));
}
inline bool cmp(const Point &a,const Point &b)
{
    if(a.x==b.x)return a.y<b.y;
    return a.x<b.x;
}
inline int convexHull()
{
    sort(p+1,p+n+1,cmp);
    int m=0;
    for(register int i=1;i<=n;i++)
    {
        while(m>=2&&Cross(p[q[m]]-p[q[m-1]],p[i]-p[q[m-1]])<=0)m--;
        q[++m]=i;
    }
    int k=m;
    for(register int i=n-1;i>=1;i--)
    {
        while(m>k&&Cross(p[q[m]]-p[q[m-1]],p[i]-p[q[m-1]])<=0)m--;
        q[++m]=i;
    }
    if(n>1)m--;
    return m;
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&L); 
        for(register int i=1;i<=n;i++)scanf("%lf%lf",&p[i].x,&p[i].y);
        int m=convexHull();double ans=0;
        for(register int i=2;i<=m;i++)ans+=lenght(p[q[i]],p[q[i-1]]);
        ans+=lenght(p[q[m]],p[q[1]]);
        ans+=2*pi*L;
        printf("%.0lf\n",ans);
        if(T)printf("\n"); 
    }
    return 0;
}

 

posted @ 2018-01-17 22:26  yoshinow2001  阅读(114)  评论(0编辑  收藏  举报