跳到正文

题解

题解:P4490 [CTSC2009] 魔幻花园

h120316约 16 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 16 分钟
题目
P4490

版本

  1. v1当前

#include <bits/stdc++.h> template <typename T> inline void checkMax(T &x, T y) { x = x > y ? x : y; } template <typename T> inline void checkMin(T &x, T y) { x = x < y ? x : y; } typedef long long ll; typedef unsigned long long ull; typedef long double ld; #define double ld

std::mt19937 rnd((unsigned)time(NULL)); const double eps = 1e-6, pi = acos(-1.0), inf = 1e100; inline double sign(double x) { return x > eps ? 1 : (x < -eps ? -1 : 0); }

struct poly { double a, b, c; poly() { a = b = c = 0; } poly(double a, double b, double c) : a(a), b(b), c(c) {} friend poly operator+(const poly &f, const poly &g) { return poly(f.a + g.a, f.b + g.b, f.c + g.c); } friend poly operator-(const poly &f, const poly &g) { return poly(f.a - g.a, f.b - g.b, f.c - g.c); } friend poly operator*(const poly &f, const poly &g) { return poly(f.a * g.c + f.b * g.b + f.c * g.a, f.b * g.c + f.c * g.b, f.c * g.c); } double getValue(double x) const { return a * x * x + b * x + c; } double getMinValue(double L, double R) { double res = std::min(getValue(L), getValue(R)); if (sign(a) == 0) { return res; } double mid = -b / (2 * a); if (mid >= L && mid <= R) { return std::min(res, getValue(mid)); } else { return res; } } auto getRoot() const { if (sign(a) == 0) { if (sign(b) == 0 && sign(c) == 0) return std::vector<double> {}; return std::vector<double> {-c / b}; } std::vector<double> res; double dlt = b * b - 4 * a * c; if (dlt < 0) return res; dlt = sqrt(dlt); double x1 = (-b - dlt) / (2 * a), x2 = (-b + dlt) / (2 * a); res.emplace_back(x1); res.emplace_back(x2); return res; } void write() { std::cerr << std::fixed << ”(” << a << “)x^2 + (” << b << “)x + (” << c << ”)\n”; } };

struct Vector { double x, y; Vector() { x = y = 0; } Vector(const double &x, const double &y) : x(x), y(y) {} Vector(const Vector &t) : x(t.x), y(t.y) {} friend Vector operator-(const Vector &x, const Vector &y) { return Vector(x.x - y.x, x.y - y.y); } Vector &operator-=(const Vector &t) { x -= t.x, y -= t.y; return this; } friend Vector operator+(const Vector &x, const Vector &y) { return Vector(x.x + y.x, x.y + y.y); } Vector &operator+=(const Vector &t) { x += t.x; y += t.y; return this; } friend Vector operator/(const Vector &x, const double &y) { return Vector(x.x / y, x.y / y); } Vector &operator/=(const double t) { x /= t; y /= t; return this; } friend Vector operator(const Vector &x, const double &y) { return Vector(x.x * y, x.y * y); } friend Vector operator(const double &y, const Vector &x) { return Vector(x.x * y, x.y * y); } Vector &operator=(const double t) { x *= t; y *= t; return this; } double mod() const { return hypot(x, y); } double mod2() const { return x * x + y * y; } friend double operator(const Vector &a, const Vector &b) { return a.x * b.x + a.y * b.y; } //Dot Product friend double operator^(const Vector &a, const Vector &b) { return a.x * b.y - a.y * b.x; } //cross Product friend bool operator==(const Vector &a, const Vector &b) { return sign(a.x - b.x) == 0 && sign(a.y - b.y) == 0; } friend bool operator!=(const Vector &a, const Vector &b) { return sign(a.x - b.x) != 0 || sign(a.y - b.y) != 0; } friend bool operator<(const Vector &a, const Vector &b) { return sign(a.x - b.x) ? a.x < b.x : sign(a.y - b.y) < 0; } friend bool operator>(const Vector &a, const Vector &b) { return sign(a.x - b.x) ? a.x > b.x : sign(a.y - b.y) > 0; } friend bool operator||(const Vector &a, const Vector &b) { return sign(a ^ b) == 0; } Vector rotate(const double &theta) const { double c = cos(theta), s = sin(theta); return Vector(x * c + y * s, -x * s + y * c); } void read() { std::cin >> x >> y; } Vector normal() const { return Vector(-y, x); } double slope() const { return sign(y) ? (sign(x) ? y / x : (y > 0 ? inf : -inf)) : 0; } double angle() const { return atan2(y, x); } }; typedef Vector Point; double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; } double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; } double angle(Vector a, Vector b) { return acos((a * b) / a.mod() / b.mod()); } double dis(Point x, Point y) { return (x - y).mod(); }

typedef std::vector <Point> Polygon;

const int N = 105; double h, g; int n; Point po[N]; double ans = inf;

double cur = 0; struct dPoint { poly x, y; dPoint() {} dPoint(const poly &x, const poly &y) : x(x), y(y) {} Point getPoint() const { return Point(x.getValue(cur), y.getValue(cur)); } friend bool operator<(const dPoint &x, const dPoint &y) { return x.getPoint() < y.getPoint(); } friend bool operator==(const dPoint &x, const dPoint &y) { return x.getPoint() == y.getPoint(); } friend dPoint operator-(const dPoint &x, const dPoint &y) { return dPoint(x.x - y.x, x.y - y.y); } friend poly operator^(const dPoint &a, const dPoint &b) { return a.x * b.y - a.y * b.x; } //cross Product } P[N];

struct Import { double t; int i, j, k; Import() {} Import(double t, int i, int j, int k) : t(t), i(i), j(j), k(k) {} }; std::vector<Import> imp;

int id[N];

std::vector<int> getConvexHull() { std::vector<int> res(0); res.emplace_back(id[1]); res.emplace_back(id[2]); for (int i = 3; i <= n; ++i) { while (int(res.size()) > 1 && sign(cross(po[res[int(res.size()) - 1]] - po[res[int(res.size()) - 2]], po[id[i]] - po[res[int(res.size()) - 1]])) >= 0) { res.pop_back(); } res.emplace_back(id[i]); } return res; }; poly area_X;

int main() { #ifdef LOCAL_JUDGE freopen(“0.in”, “r”, stdin); #endif std::ios::sync_with_stdio(false); std::cin.tie(nullptr);

C++
std::cin >> h >> g;
std::cin >> n;
for (int i = 1; i <= n; ++i) {
    Point A, B;
    A.read(), B.read();
    id[i] = i;
    P[i].x = poly(0, B.x - A.x, A.x);
    P[i].y = poly(0, B.y - A.y, A.y);
}

if (n < 2) {
    std::cout << "0\n";
}

for (int i = 1; i <= n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
        for (int k = j + 1; k <= n; ++k) {
            auto vec = ((P[j] - P[i]) ^ (P[k] - P[i])).getRoot();
            for (auto t : vec) {
                if (t >= 0 && t <= 1) {
                    imp.emplace_back(t, i, j, k);
                }
            }
        }
    }
}
imp.emplace_back(0, 0, 0, 0);
std::sort(imp.begin(), imp.end(), [&](const Import &A, const Import &B) {
    return A.t < B.t;
});

int ct = imp.size();
imp.emplace_back(1, 0, 0, 0);
std::vector<int> vec;
for (int i = 0; i < ct; ++i) {
    double real_cur = imp[i].t;
    cur = (imp[i].t + imp[i + 1].t) / 2.0;
    area_X = poly();
    for (int i = 1; i <= n; ++i) {
        po[i] = P[i].getPoint();
    }
    std::sort(id + 1, id + n + 1, [&](int i, int j) {
        return po[i] < po[j];
    });
    vec = getConvexHull();
    for (int j = 0; j < int(vec.size()) - 1; ++j) {
        area_X = area_X + (P[vec[j + 1]] ^ P[vec[j]]);
    }
    std::reverse(id + 1, id + n + 1);
    vec = getConvexHull();
    for (int j = 0; j < int(vec.size()) - 1; ++j) {
        area_X = area_X + (P[vec[j + 1]] ^ P[vec[j]]);
    }
    cur = real_cur;
    checkMin(ans, area_X.getMinValue(cur, imp[i + 1].t));
}

std::cout << std::fixed << std::setprecision(10) << ans * 0.5 << '\n';

return 0;

}

原文