POJ 1113 Wall 凸包模板题

https://vjudge.net/contest/250229#problem/A

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
#define eps 1e-7

#define PI acos(-1.0)

using namespace std;

struct node
{
    int x,y;
} a[1010],p[1010];

int top, n;

double cross(node p0,node p1,node p2)
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}

double dis(node a,node b)//same angle with a[0]
{
    return ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

bool cmp(node p1,node p2)
{
    double z=cross(a[0],p1,p2);
    if(z>0||(z==0&&dis(a[0],p1)<dis(a[0],p2)))
        return 1;
    return 0;
}

void Graham()
{
    int k=0;
    for(int i=0; i<n; i++)
        if(a[i].y<a[k].y||(a[i].y==a[k].y&&a[i].x<a[k].x))
            k=i;

    swap(a[0],a[k]);
    sort(a+1,a+n,cmp);

    top=1;
    p[0]=a[0];
    p[1]=a[1];
    for(int i=2; i<n; i++)
    {
        while(cross(p[top-1],p[top],a[i])<0&&top)
            top--;
        top++;
        p[top]=a[i];
    }
}

int main()
{
    int r;
    while(~scanf("%d%d",&n,&r))
    {
        top = 0;
        for(int i=0; i<n; i++)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
        }

        Graham();

        double sum = 0;
        for(int i=0; i<top; i++)
        {
            sum += sqrt(dis(p[i],p[i+1]));
        }
        sum += sqrt(dis(p[0],p[top]));
        double resu = sum + 2 * PI * r;
        cout << floor(resu+0.5) <<endl;
    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/ronnielee/p/9545578.html