cogs896 圈奶牛(凸包)

描述

农夫约翰想要建造一个围栏用来围住他的奶牛,可是他资金匮乏。他建造的围栏必须包括他的奶牛喜欢吃草的所有地点。对于给出的这些地点的坐标,计算最短的能够围住这些点的围栏的长度。

PROGRAM NAME: fc

INPUT FORMAT(file fc.in)
输入数据的第一行包括一个整数 N。N(0 <= N <= 10,000)表示农夫约翰想要围住的放牧点的数目。接下来 N 行,每行由两个实数组成,Xi 和 Yi,对应平面上的放牧点坐标(-1,000,000 <= Xi,Yi <= 1,000,000)。数字用小数表示。

OUTPUT FORMAT(file fc.out)
输出必须包括一个实数,表示必须的围栏的长度。答案保留两位小数。

SAMPLE INPUT (file fc.in)
4
4 8
4 12
5 9.3
7 8
SAMPLE OUTPUT (file fc.out)
12.00

凸包裸体,人生中第一次计算几何(一A O(∩_∩)O~)
推荐博客:http://www.yhzq-blog.cc/cogs-896-圈奶牛/

这里写代码片
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>

using namespace std;

const double eps=1e-7;
struct node{
    double x,y;
};
node po[10010];
int top,sta[20010];
int n;

int dcmp(double x)  //精度控制 
{
    if (fabs(x)<eps) return 0;
    else 
       if (x>0) return 1;
       else return -1;
}

int cmp(const node &a,const node &b)
{
    if (dcmp(a.x-b.x)!=0) return a.x<b.x;
    else return a.y<b.y; 
}

double cross(int bh1,int bh2,int bh3)  //叉积 x1*y2-x2*y1; 
{
    return (po[bh1].x-po[bh3].x)*(po[bh2].y-po[bh3].y)-(po[bh2].x-po[bh3].x)*(po[bh1].y-po[bh3].y);
}

double dis(int bh1,int bh2)
{
    return (double)sqrt((po[bh1].x-po[bh2].x)*(po[bh1].x-po[bh2].x)+(po[bh1].y-po[bh2].y)*(po[bh1].y-po[bh2].y));
}

void TuB()
{
    int i;
    top=0;
    for (i=1;i<=n;i++)
    {  //如果待加入的点在栈顶点的左侧,栈顶元素出战 
        while (top>1&&dcmp(cross(i,sta[top],sta[top-1]))<=0) top--;
        sta[++top]=i;
    }  //下凸壳 
    int k=top;
    for (i=n-1;i>=1;i--)
    {
        while (top>k&&dcmp(cross(i,sta[top],sta[top-1]))<=0) top--;
        sta[++top]=i;
    }  //上凸壳 
    if (n>1) top--;  //如果n>1,1号点会被算两遍,所以这里需要-- 
    double ans=0;
    ans+=dis(sta[1],sta[top]);
    while (top>1) ans+=dis(sta[top],sta[top-1]),top--;  //top>1!!! 
    printf("%0.2lf",ans);
    return;
}

int main()
{
    freopen("fc.in","r",stdin); //cogs支持文件读写 
    freopen("fc.out","w",stdout);
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
        scanf("%lf%lf",&po[i].x,&po[i].y);
    sort(po+1,po+1+n,cmp);  //水平排序,操作简单 
    TuB();
    return 0;
} 
原文地址:https://www.cnblogs.com/wutongtong3117/p/7673625.html