zoukankan      html  css  js  c++  java
  • hoj1392

    凸包问题,求出凸包后,计算距离,不知道为什么n==2的情况居然不是两点距离的2倍,而是正好是两点的距离。

    View Code
    #include <iostream>
    #include
    <cstdio>
    #include
    <cstdlib>
    #include
    <cstring>
    #include
    <algorithm>
    #include
    <cmath>
    using namespace std;

    #define maxn 505

    struct point
    {
    double x, y;
    }pnt[maxn], res[maxn];

    int n, m;

    bool mult(point sp, point ep, point op)
    {
    return (sp.x - op.x) * (ep.y - op.y)>= (ep.x - op.x) * (sp.y - op.y);
    }

    bool operator < (const point &l, const point &r)
    {
    return l.y < r.y || (l.y == r.y && l.x < r.x);
    }

    void input()
    {
    for (int i = 0;i < n; i++)
    {
    scanf(
    "%lf%lf", &pnt[i].x, &pnt[i].y);
    }
    }

    int graham()
    {
    int i, len, top = 1;
    sort(pnt, pnt
    + n);
    if (n == 0) return 0; res[0] = pnt[0];
    if (n == 1) return 1; res[1] = pnt[1];
    if (n == 2) return 2; res[2] = pnt[2];
    for (i = 2; i < n; i++)
    {
    while (top && mult(pnt[i], res[top], res[top-1]))
    top
    --;
    res[
    ++top] = pnt[i];
    }
    len
    = top; res[++top] = pnt[n - 2];
    for (i = n - 3; i >= 0; i--)
    {
    while (top!=len && mult(pnt[i], res[top], res[top-1])) top--;
    res[
    ++top] = pnt[i];
    }
    return top; // 返回凸包中点的个数
    }

    double dist(point p1,point p2)
    {
    return (sqrt((p1.x-p2.x) * (p1.x-p2.x) + (p1.y-p2.y) * (p1.y-p2.y)));
    }

    double work()
    {
    double ret = dist(res[m - 1], res[0]);
    for (int i = 0; i < m - 1; i++)
    ret
    += dist(res[i], res[i + 1]);
    if (n == 2)
    return ret / 2;
    return ret;
    }

    int main()
    {
    //freopen("D:\\t.txt", "r", stdin);
    while (scanf("%d", &n) != EOF && n != 0)
    {
    input();
    m
    = graham();
    printf(
    "%.2f\n", work());
    }
    return 0;
    }
  • 相关阅读:
    BZOJ 1391: [Ceoi2008]order
    BZOJ 4504: K个串
    2019 年百度之星·程序设计大赛
    POJ 2398 Toy Storage (二分 叉积)
    POJ 2318 TOYS (二分 叉积)
    HDU 6697 Closest Pair of Segments (计算几何 暴力)
    HDU 6695 Welcome Party (贪心)
    HDU 6693 Valentine's Day (概率)
    HDU 6590 Code (判断凸包相交)
    POJ 3805 Separate Points (判断凸包相交)
  • 原文地址:https://www.cnblogs.com/rainydays/p/1999202.html
Copyright © 2011-2022 走看看