AtCoder ABC 365G 凸包 + 二分

题意

AtCoder ABC 365G Freestyle

题解

考虑任两种操作 ( A i , B i ) (A_{i},B_{i}) (Ai,Bi)和 ( A j , B j ) (A_{j},B_{j}) (Aj,Bj),则他们的任意组合可以表示为 ( t A i + ( 1 − t ) A j , t B i + ( 1 − t ) B j ) \big(tA_{i}+(1-t)A_{j},tB_{i}+(1-t)B_{j}\big) (tAi+(1−t)Aj,tBi+(1−t)Bj),那么将其看作二维平面的点,即 x = B , y = A x = B, y = A x=B,y=A,则这两种操作对应点的连线都可以被取到。拓展到两个点以上的情况,则可以任取这些点构成的凸包内的任一点。

对于约束 D y − C x ≤ 0 Dy - Cx \leq 0 Dy−Cx≤0,则可以取使得 y / x y/x y/x最小的节点 s s s,若此节点不满足约束条件,则任一节点都不满足条件。对于最小化时间,则转化为最大化 x x x,那么可以取使 x x x最大化的节点 t t t,若 t t t满足约束条件,则单位为 t t t对应的时间。

其余情况,我们考虑凸包 s → t s\rightarrow t s→t的节点,二分满足约束条件的节点,最后再二分该节点与凸包上次一节点的线性组合。总时间复杂度 O ( n + q log ⁡ n ) O(n + q\log n) O(n+qlogn)。

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
template <typename T>
struct Point {
    T x, y;
    Point operator+(Point rhs) {
        return {x + rhs.x, y + rhs.y};
    }
    Point operator-(Point rhs) {
        return {x - rhs.x, y - rhs.y};
    }
    T dot(Point rhs) {
        return x * rhs.x + y * rhs.y;
    }
    T det(Point rhs) {
        return x * rhs.y - y * rhs.x;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    using P = Point<long long>;
    vector<P> ps(n);
    for (int i = 0; i < n; ++i) {
        cin >> ps[i].y >> ps[i].x;
    }
    auto get_convex_hull = [&]() {
        sort(ps.begin(), ps.end(), [&](const auto& lhs, const auto& rhs) {
            if (lhs.x != rhs.x) {
                return lhs.x < rhs.x;
            }
            return lhs.y < rhs.y;
        });
        vector<P> qs(n * 2);
        int k = 0;
        for (int i = 0; i < n; ++i) {
            while (k > 1 && (qs[k - 1] - qs[k - 2]).det(ps[i] - qs[k - 1]) <= 0) {
                k -= 1;
            }
            qs[k++] = ps[i];
        }
        for (int i = n - 2, t = k; i >= 0; --i) {
            while (k > t && (qs[k - 1] - qs[k - 2]).det(ps[i] - qs[k - 1]) <= 0) {
                k -= 1;
            }
            qs[k++] = ps[i];
        }
        qs.resize(k - 1);
        return qs;
    };
    if (n > 1) {
        ps = get_convex_hull();
        n = ps.size();
    }
    int s = -1, t = -1;
    for (int i = 0; i < n; ++i) {
        if (s == -1 || ps[i].y * ps[s].x < ps[s].y * ps[i].x) {
            s = i;
        }
        if (t == -1 || ps[t].x < ps[i].x) {
            t = i;
        }
    }

    int q;
    cin >> q;
    while (q--) {
        int c, d;
        cin >> c >> d;

        auto judge = [&](P& p) {
            return d * p.y - c * p.x <= 0;
        };
        if (!judge(ps[s])) {
            cout << -1 << '\n';
            continue;
        }
        if (judge(ps[t])) {
            cout << fixed << setprecision(11) << (double)d / ps[t].x << '\n';
            continue;
        }

        auto get_idx = [&]() {
            int lb = s, ub = t;
            while (ub - lb > 1) {
                int mid = (lb + ub) / 2;
                if (judge(ps[mid])) {
                    lb = mid;
                } else {
                    ub = mid;
                }
            }
            return lb;
        };
        int idx = get_idx();

        double lb = 0, ub = 1;
        for (int i = 0; i < 50; ++i) {
            double mid = (lb + ub) / 2;
            double x = ps[idx].x + (ps[(idx + 1) % n].x - ps[idx].x) * mid;
            double y = ps[idx].y + (ps[(idx + 1) % n].y - ps[idx].y) * mid;
            if (d * y - c * x <= 0) {
                lb = mid;
            } else {
                ub = mid;
            }
        }
        double x = ps[idx].x + (ps[(idx + 1) % n].x - ps[idx].x) * lb;
        cout << fixed << setprecision(11) << d / x << '\n';
    }

    return 0;
}
相关推荐
147API7 分钟前
Claude global workspace 研究给智能体测试提了一个醒
人工智能·算法·机器学习
葫三生8 分钟前
《论三生原理》与模糊数学的关联、异同、互补关系?
人工智能·科技·算法·机器学习·开源
我命由我1234518 分钟前
方差(实例实操、与标准差的区别)
java·数据结构·算法·数据分析·java-ee·intellij-idea·idea
闲研随记18 分钟前
【文献阅读 ICML 2026】RL算法:Critique-GRPO
论文阅读·算法·llm·强化学习·rl·icml·grpo
民乐团扒谱机41 分钟前
【微实验】从迷雾中看清世界:卡尔曼滤波的数学诗意与MATLAB实践
人工智能·算法·matlab·卡尔曼滤波·kalman
什巳1 小时前
JAVA练习275-乘积最大子数组
java·开发语言·数据结构·算法
@踏雪寻梅1 小时前
27. 移除元素
算法
小牛不牛的程序员1 小时前
利用Claude Code构建自动化需求挖掘工具:从原理到实践
java·算法·自动化
Mr.HeBoYan1 小时前
DPDK为什么越来越少使用rte_ring?——从Lock-Free到Run-to-Completion,重新理解现代DPDK架构演进(上)
linux·网络·算法·性能优化·架构·dpdk
在书中成长1 小时前
HarmonyOS 小游戏《对战五子棋》开发第14篇 - 困难AI实现:Minimax算法原理详解
人工智能·算法·harmonyos