2020暑期训练8

基础

const double eps = 1e-8;

int sgn(double x)//判断浮点数符号
{
    if (fabs(x) < eps)
        return 0;
    if (x < 0)
        return -1;
    return 1;
}
struct point
{
    double x, y;
    point() {}
    point(double x, double y) : x(x), y(y) {}
    point operator+(const point &p) const
    {
        point new_p(x + p.x, y + p.y);
        return new_p;
    }
    point operator-(const point &p) const
    {
        point new_p(x - p.x, y - p.y);
        return new_p;
    }
    point operator|(const double &p) const //数乘
    {
        point new_p(x * p, y * p);
        return new_p;
    }
    point operator/(const double &p) const //同数乘
    {
        point new_p(x / p, y / p);
        return new_p;
    }
    bool operator==(const point &p) const
    {
        return sgn(x - p.x) == 0 && sgn(y - p.y) == 0;
    }
    double operator*(const point &p) const //点乘
    {
        return x * p.x + y * p.y;
    }
    double operator^(const point &p) const //叉乘
    {
        return x * p.y - y * p.x;
    }
    double len() //原点距离
    {
        return sqrt(x * x + y * y);
    }
    void print()
    {
        cout << x << ' ' << y << '\n';
    }
};
double dist(const point &a, const point &b) //两点距离
{
    return sqrt((a - b) * (a - b));
}
//typedef point vec; 向量也可以这样表示

printf 输出双精度浮点型数一定要用 %f,用 %lf 在一些情况下只会输出 -0.000000

凸包

Graham算法

找出最左下角的点,排序,然后一个一个连过去,如果新边的产生是逆时针的就入栈,如果是顺时针的就不断弹出(用叉积判断)。最后栈中的点就是凸包边界的点。

Andrew算法

按x,y轴坐标排序,依次向后连边,如果下一个点在上一条边的左边就入栈,在右边就不断出栈,可以得到半个凸壳。在另外半边也这样操作即可。

例题:P2742 [USACO5.1]圈奶牛Fencing the Cows /【模板】二维凸包
模板题,测试一下代码正确性

#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const int maxn = 1e5 + 10;
int sgn(double x)
{
    if (fabs(x) < eps)
        return 0;
    if (x < 0)
        return -1;
    return 1;
}
struct point
{
    double x, y;
    point() {}
    point(double x, double y) : x(x), y(y) {}
    point operator+(const point &p) const
    {
        point new_p(x + p.x, y + p.y);
        return new_p;
    }
    point operator-(const point &p) const
    {
        point new_p(x - p.x, y - p.y);
        return new_p;
    }
    point operator|(const double &p) const //scalar multiplication
    {
        point new_p(x * p, y * p);
        return new_p;
    }
    point operator/(const double &p) const
    {
        point new_p(x / p, y / p);
        return new_p;
    }
    bool operator==(const point &p) const
    {
        return sgn(x - p.x) == 0 && sgn(y - p.y) == 0;
    }
    double operator*(const point &p) const //dot product
    {
        return x * p.x + y * p.y;
    }
    double operator^(const point &p) const //cross product
    {
        return x * p.y - y * p.x;
    }
    double len()
    {
        return sqrt(x * x + y * y);
    }
    void print()
    {
        cout << x << ' ' << y << '\n';
    }
};
double dist(const point &a, const point &b)
{
    return sqrt((a - b) * (a - b));
}
//typedef point vec;
point pt[maxn];
int Stack[maxn], top;
//相对于list[0]的极角排序
bool _cmp(point p1, point p2)
{
    double tmp = (p1 - pt[0]) ^ (p2 - pt[0]);
    if (sgn(tmp) > 0)
        return true;
    else if (sgn(tmp) == 0 && sgn(dist(p1, pt[0]) - dist(p2, pt[0])) <= 0)
        return true;
    else
        return false;
}
void Graham(int n)
{
    point p0;
    int k = 0;
    p0 = pt[0];
    //找最下边的一个点
    for (int i = 1; i < n; i++)
    {
        if ((p0.y > pt[i].y) || (p0.y == pt[i].y && p0.x > pt[i].x))
        {
            p0 = pt[i];
            k = i;
        }
    }
    swap(pt[k], pt[0]);
    sort(pt + 1, pt + n, _cmp);
    if (n == 1)
    {
        top = 1;
        Stack[0] = 0;
        return;
    }
    if (n == 2)
    {
        top = 2;
        Stack[0] = 0;
        Stack[1] = 1;
        return;
    }
    Stack[0] = 0;
    Stack[1] = 1;
    top = 2;
    for (int i = 2; i < n; i++)
    {
        while (top > 1 && sgn((pt[Stack[top - 1]] - pt[Stack[top - 2]]) ^ (pt[i] - pt[Stack[top - 2]])) <= 0)
            top--;
        Stack[top++] = i;
    }
}
int main()
{
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
        scanf("%lf%lf", &pt[i].x, &pt[i].y);
    Graham(n);
    double ans = 0.0;
    Stack[top] = Stack[0];
    for (int i = 0; i < top; ++i)
        ans += dist(pt[Stack[i]], pt[Stack[i + 1]]);
    printf("%.2lf\n", ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值