凸包模板!!

来源:互联网 发布:中国网络暴力现状 编辑:程序博客网 时间:2024/06/03 20:33
#include<map>#include<set>#include<list>#include<cmath>#include<ctime>#include<queue>#include<stack>#include<cctype>#include<cstdio>#include<string>#include<vector>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>#define MAXN 1005#define INF 0x3f3f3f3f#define LL long long#define DBL double#define eps 1e-6#define PI acos(-1.0)#define Test() cout<<"Test"<<endl;#define Debug(a) cout<<#a<<" = "<<a<<endl;#define Debug2(a,b) cout<<#a<<" = "<<a<<" , "<<#b<<" = "<<b<<endl;using namespace std;struct P{    DBL x, y;};int n, L;P p[MAXN];bool cmp(P a, P b){    return a.y<b.y || (a.y==b.y && a.x<b.x);}DBL mul(DBL t){    return t*t;}DBL dis(P a, P b){    return sqrt(mul(a.x-b.x)+mul(a.y-b.y));}int dblcmp(DBL x){    return fabs(x)<eps? 0: x>0? 1: -1;}DBL cross(P a, P b, P c){        //ab X ac    return (b.x-a.x)*(c.y-a.y) - (c.x-a.x)*(b.y-a.y);}DBL grahamScan(){                //水平序     sort(p, p+n, cmp);    int top=-1, s[MAXN], tmp;    s[++top]=0, s[++top]=1;     for(int i=2; i<n; i++){        // 做右链        while(top>0 && dblcmp(cross(p[s[top-1]], p[s[top]], p[i])) <= 0) top --;        s[++top] = i;    }    tmp = top;                    // 此时的栈顶元素一定是第n个点    s[++top]=n-2;    for(int i=n-3; i>=0; i--){    // 做左链        while(top>tmp && dblcmp(cross(p[s[top-1]], p[s[top]], p[i])) <= 0) top --;        s[++top] = i;    }                            // 此时的栈顶元素一定是第1个点,即s[top]=s[0]    // s[0~top-1]即为所求凸包     DBL res=0;    for(int i=0; i<top; i++)        res += dis(p[s[i]], p[s[i+1]]);    return res+2*PI*L;}int main(){    while(cin >> n >> L){        for(int i=0; i<n; i++)            scanf("%lf%lf", &p[i].x, &p[i].y);        printf("%d\n", (int)(grahamScan()+0.5));      //+0.5          }    return 0;}

0 0
原创粉丝点击