P2742 【模板】二维凸包 / [USACO5.1]圈奶牛Fencing the Cows

风格不统一 提交于 2019-11-27 03:40:28

凸包模板

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstring>
#include <queue>
#include <cmath>
using namespace std;
const int maxn = 1e4 + 10 ;
struct node
{
    double x , y ;
}a[maxn];
bool cmp(node x ,node y)
{
    if(x.x!=y.x)
        return x.x < y.x ;
    else
        return x.y < y.y ;
}
node p[maxn] ;
double dfs(node a ,node b,node c,node d)
{
    double x1 = b.x - a.x ;
    double y1 = b.y - a.y ;
    double x2 = d.x - c.x ;
    double y2 = d.y - c.y ;
    return x1*y2-x2*y1;
}
double qqq(node a,node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{
    int n ;
    scanf("%d",&n);
    double sum = 0 ;
    for(int i = 0 ;i < n; i++)
    {
        scanf("%lf %lf",&a[i].x,&a[i].y);
    }
    sort(a,a+n,cmp);
    int k = 0;
    p[k++] = a[0] ;
    for(int i = 1; i < n; i++)
    {
        while(k>1&&dfs(p[k-1],p[k-2],a[i],p[k-1])<=0)
        {
            sum-=qqq(p[k-1],p[k-2]);
            k--;
        }
        p[k++] = a[i] ;
        sum+= qqq(p[k-1],p[k-2]);
    }
    int t  = k ;
    for(int i = n-2; i >=0 ; i--)
    {
        while(k>t&&dfs(p[k-1],p[k-2],a[i],p[k-1])<=0)
        {
             sum-=qqq(p[k-1],p[k-2]);
            k--;
        }
        p[k++] = a[i] ;
        sum+= qqq(p[k-1],p[k-2]);
    }
    printf("%.2lf\n",sum);
    return 0;
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!