题意
题解
考虑任两种操作 ( 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)。
#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;
}