Hedwig's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View on GitHub

:heavy_check_mark: test/polygon_diameter.test.cpp

Depends on

Code

#include <bits/stdc++.h>
using namespace std;

#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/4/CGL_4_B"
#define ERROR 1e-9
#include "../math/polygon.cpp"

using P = Point<long double>;

int main() {
    cout << fixed << setprecision(20);

    int n;
    cin >> n;
    vector<P> A(n);
    for (int i = 0; i < n; i++)
        cin >> A[i];
    long double d = diameter(convex_hull(A));
    cout << d << '\n';
    return 0;
}
#line 1 "test/polygon_diameter.test.cpp"
#include <bits/stdc++.h>
using namespace std;

#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/4/CGL/4/CGL_4_B"
#define ERROR 1e-9
#line 3 "math/polygon.cpp"
using namespace std;

#line 3 "math/point.cpp"
using namespace std;

constexpr long double GEOMETRY_EPS = 1e-8;

// sgn
// a > 0なら1, a = 0なら0,a < 0なら-1を返す.
constexpr inline int sgn(const long double a) { return (a < -GEOMETRY_EPS ? -1 : (a > GEOMETRY_EPS ? 1 : 0)); }
constexpr inline int sgn(const int a) { return (a < 0 ? -1 : (a > 0 ? 1 : 0)); }

// 2次元座標クラス
// T = int,long doubleなど
template <typename T>
struct Point {
    T x, y;
    constexpr inline Point(T x = 0, T y = 0) : x(x), y(y) {}

    // unary operator: +,-
    constexpr inline Point operator+() const { return *this; }
    constexpr inline Point operator-() const { return Point(-x, -y); }

    // +=,-=,*=,/=
    constexpr inline Point &operator+=(const Point &q) {
        x += q.x;
        y += q.y;
        return *this;
    }
    constexpr inline Point &operator-=(const Point &q) {
        x -= q.x;
        y -= q.y;
        return *this;
    }
    template <typename U>
    constexpr inline Point &operator*=(U a) {
        x *= a;
        y *= a;
        return *this;
    }
    template <typename U>
    constexpr inline Point &operator/=(U a) {
        x /= a;
        y /= a;
        return *this;
    }

    // +,-,*,/
    constexpr inline Point operator+(const Point &q) const { return Point(*this) += q; }
    constexpr inline Point operator-(const Point &q) const { return Point(*this) -= q; }
    template <typename U>
    constexpr inline Point operator*(const U &a) const { return Point(*this) *= a; }
    template <typename U>
    constexpr inline Point operator/(const U &a) const { return Point(*this) /= a; }

    // <,> の比較は辞書順の比較, つまりx,yの順に大きい方を確認する.
    inline bool operator<(const Point &q) const { return (sgn(x - q.x) != 0 ? sgn(x - q.x) < 0 : sgn(y - q.y) < 0); }
    inline bool operator>(const Point &q) const { return (sgn(x - q.x) != 0 ? sgn(x - q.x) > 0 : sgn(y - q.y) > 0); }
    inline bool operator==(const Point &q) const { return (sgn(x - q.x) == 0 && sgn(y - q.y) == 0); }

    friend ostream &operator<<(ostream &os, const Point &p) { return os << p.x << ' ' << p.y; }
    friend istream &operator>>(istream &is, Point &p) { return is >> p.x >> p.y; }
};

// *,/
template <typename T, typename U>
inline Point<T> operator*(const U &s, const Point<T> &p) { return {s * p.x, s * p.y}; }
template <typename T, typename U>
inline Point<T> operator/(const U &s, const Point<T> &p) { return {p.x / s, p.y / s}; }

// dot
// p,qの内積を計算する.
template <typename T>
constexpr inline T dot(const Point<T> &p, const Point<T> &q) { return p.x * q.x + p.y * q.y; }

// cross
// p,qの外積を計算する
template <typename T>
constexpr inline T cross(const Point<T> &p, const Point<T> &q) { return p.x * q.y - q.x * p.y; }

// length2
// ベクトルpの長さ(原点からの距離)の2乗を求める.
template <typename T>
constexpr inline T length2(const Point<T> &p) { return dot(p, p); }

// length
// ベクトルpの長さ(原点からの距離)を求める.
template <typename T>
inline long double length(const Point<T> &p) { return sqrt((long double)length2(p)); }

// dist
// 点pと点qの間の距離を求める.
template <typename T>
inline long double dist(const Point<T> &p, const Point<T> &q) { return length(p - q); }

// sgn_area
// p,q,rがつくる三角形の符号付き面積
template <typename T>
constexpr inline long double sgn_area(const Point<T> &p, const Point<T> &q, const Point<T> &r) { return (long double)cross(q - p, r - p) / 2.0; }

// area
// p,q,rがつくる三角形の面積
template <typename T>
constexpr inline long double area(const Point<T> &p, const Point<T> &q, const Point<T> &r) { return abs(sgn_area(p, q, r)); }

// normalize
// 点pを長さ1に正規化した点を返す.
template <typename T>
inline Point<long double> normalize(const Point<T> &p) { return (Point<long double>)p / length(p); }

// rotation
// 点pを反時計回りにargだけ回転させた点を返す. (argはradで測る)
template <typename T>
inline Point<long double> rotation(const Point<T> &p, double arg) { return Point(cos(arg) * p.x - sin(arg) * p.y, sin(arg) * p.x + cos(arg) * p.y); }

// angle
// 点pのx軸の正の方向から反時計回りに測った角度を[-pi,pi]で返す.
template <typename T>
inline long double angle(const Point<T> &p) { return atan2(p.y, p.x); }

// rot90
// 点pを反時計回りに90度回転
template <typename T>
constexpr inline Point<T> rot90(const Point<T> &p) { return Point(-p.y, p.x); }

// iSP
// 異なる3点a,b,cの位置関係を返す.
template <typename T>
int iSP(const Point<T> &a, const Point<T> &b, const Point<T> &c) {
    if (sgn(cross(c - b, a - b)) > 0) return 1;  // ab bc __/: +1
    if (sgn(cross(c - b, a - b)) < 0) return -1; // ab bc --\: -1
    if (sgn(dot(a - b, c - b)) < 0) return 2;    // abc   ---: +2
    if (sgn(dot(a - c, b - c)) < 0) return -2;   // acb   ---: -2
    return 0;                                    // bac   ---:  0
}

// example:
// using P = Point<int>;
// using P = Point<long double>;
#line 6 "math/polygon.cpp"

// convex_hull
// 点集合pointsの凸包を求める.
// 求めた凸包は最も左にあるもののうち最も下にあるものから順に反時計回りに並んでいる.
template <typename T>
vector<Point<T>> convex_hull(vector<Point<T>> points) {
    sort(points.begin(), points.end());
    int n = (int)points.size(), k = 0;
    vector<Point<T>> convex(2 * n);
    for (int i = 0; i < n; i++) {
        while (k > 1 && iSP(convex[k - 2], convex[k - 1], points[i]) == -1)
            k--;
        convex[k++] = points[i];
    }
    for (int i = n - 2, t = k; i >= 0; i--) {
        while (k > t && iSP(convex[k - 2], convex[k - 1], points[i]) == -1)
            k--;
        convex[k++] = points[i];
    }
    convex.resize(k - 1);
    return convex;
}

// polygon_area
// pointsのn点からなる多角形Gの面積を求める.
// Gはpoints[i],points[(i + 1)%n]の2つの頂点を結ぶ線分を
// 辺とする多角形である.
template <typename T>
long double polygon_area(const vector<Point<T>> &points) {
    int n           = (int)points.size();
    long double ret = 0;
    for (int i = 0; i + 2 < n; i++)
        ret += sgn_area(points[0], points[i + 1], points[i + 2]);
    return abs(ret);
}

// diameter
// 凸多角形convexの直径(最遠点対の距離)を求める.
template <typename T>
long double diameter(const vector<Point<T>> &convex) {
    int n = (int)convex.size();
    if (n == 2)
        return dist(convex[0], convex[1]);

    int i = 0, j = 0;
    for (int k = 0; k < n; k++) {
        if (convex[i] < convex[k]) i = k;
        if (convex[j] > convex[k]) j = k;
    }

    long double ret = 0;
    int si = i, sj = j;
    while (i != sj || j != si) {
        ret = max(ret, dist(convex[i], convex[j]));
        if (sgn(cross(convex[(i + 1) % n] - convex[i], convex[(j + 1) % n] - convex[j])) < 0)
            i = (i + 1) % n;
        else
            j = (j + 1) % n;
    }
    return ret;
}
#line 7 "test/polygon_diameter.test.cpp"

using P = Point<long double>;

int main() {
    cout << fixed << setprecision(20);

    int n;
    cin >> n;
    vector<P> A(n);
    for (int i = 0; i < n; i++)
        cin >> A[i];
    long double d = diameter(convex_hull(A));
    cout << d << '\n';
    return 0;
}
Back to top page