Wall--POJ1113(极角排序+求凸包)

http://poj.org/problem?id=1113

题目大意:现在要给n个点,让你修一个围墙把这些点围起来,距离最小是l

分析  :现在就是求凸包的周长然后再加上一个圆的周长   

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<queue>

using namespace std;
#define N 1005
#define pi acos(-1.0)
#define ESP 1e-8

int s[N];

struct Point
{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y){}
    Point operator - (const Point &temp)const
    {
        return Point(x-temp.x,y-temp.y);
    }
    Point operator + (const Point &temp)const
    {
        return Point(x+temp.x,y+temp.y);
    }
    bool operator == (const Point &temp)const
    {
        return (x==temp.x && y==temp.y);
    }
    double operator * (const Point &temp)const
    {
        return (x*temp.x+y*temp.y);
    }
    int operator ^ (const Point &temp)const{
        double t=x*temp.y-y*temp.x;
        if(t>ESP)
            return 1;
        if(fabs(t)<ESP)
            return 0;
        return -1;
    }
}a[N];

double dist(Point a,Point b)
{
    return sqrt((a-b)*(a-b));
}

int cmp(Point a1,Point a2)
{
    int t=((a1-a[0])^(a2-a[0]));
    if(t==0)
        return dist(a1,a[0])<dist(a2,a[0]);
    else
        return t>0;
}
int top;
void Graham(int n)///求凸包
{
    s[0]=0;
    s[1]=1;
    top=1;
    for(int i=2;i<n;i++)
    {
        while(top>0 && ((a[i]-a[s[top]])^(a[s[top-1]]-a[s[top]]))<=0)
            top--;
        s[++top]=i;
    }

}

int main()
{
    int n,l;
    while(scanf("%d %d",&n,&l)!=EOF)
    {
        memset(s,0,sizeof(s));
        int k=0;
        for(int i=0;i<n;i++)
        {
            scanf("%lf %lf",&a[i].x,&a[i].y);
            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);

        Graham(n);
        double C=0;
        for(int i=1;i<=top;i++)
        {
            C+=dist(a[s[i]],a[s[i-1]]);
        }
        C+=dist(a[s[0]],a[s[top]]);
        C+=2*pi*l;
        printf("%.0lf
",C);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/linliu/p/5500256.html