poj1113 Wall

题目描述:

vjudge

POJ

题解:

答案就是凸包周长+2πL。

代码:

#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1050;
const double Pi = acos(-1.0);
struct Point
{
    double x,y;
    Point(){}
    Point(double x,double y):x(x),y(y){}
    Point operator + (const Point&a)const{return Point(x+a.x,y+a.y);}
    Point operator - (const Point&a)const{return Point(x-a.x,y-a.y);}
    double operator * (const Point&a)const{return x*a.x+y*a.y;}
    double operator ^ (const Point&a)const{return x*a.y-y*a.x;}
    bool operator < (const Point&a)const{return x!=a.x?x<a.x:y<a.y;}
};
typedef Point Vector;
int n;
double L;
Point p[N],s[N];
double lth(const Vector&a){return sqrt(a*a);}
int build()
{
    sort(p+1,p+1+n);
    int tl = 0;
    for(int i=1;i<=n;i++)
    {
        while(tl>1&&((s[tl]-s[tl-1])^(p[i]-s[tl-1]))<=0)tl--;
        s[++tl]=p[i];
    }
    int k = tl;
    for(int i=n-1;i>=1;i--)
    {
        while(tl>k&&((s[tl]-s[tl-1])^(p[i]-s[tl-1]))<=0)tl--;
        s[++tl]=p[i];
    }
    if(tl!=1)tl--;
    return tl;
}
int main()
{
    scanf("%d%lf",&n,&L);
    for(int i=1;i<=n;i++)
        scanf("%lf%lf",&p[i].x,&p[i].y);
    int m = build();
    double ans=0.0;
    for(int i=1;i<m;i++)
        ans+=lth(s[i+1]-s[i]);
    ans+=lth(s[1]-s[m]);
    printf("%.0lf\n",ans+Pi*L*2);
    return 0;
}
View Code

 

posted @ 2019-06-06 07:36  LiGuanlin  阅读(209)  评论(0编辑  收藏  举报