解題思路
求兩群內公切支撐線的交點。
依x排序分半,分別建凸包A、B。因A所有x小於B,任何跨群向量B−A的x皆為正。
全體跨群向量的凸包等於B與−A的Minkowski和,可把兩凸包由最低頂點起,依邊方向角線性合併。保留每個和頂點來源的A、B接觸點。
最小與最大斜率向量對應兩條內公切線:極小斜率時所有A點在線下、所有B點在線上,極大時反之。因此只需枚舉差多邊形頂點的dy/dx,找兩極端,再以精確行列式求直線交點及half-up兩位。
時間 O(n log n)次大整數幾何運算,空間 O(n);Minkowski合併為O(n),另計座標位數。
Step-by-step animation
兩個三角形的內公切線示例
步驟 1 / 3
步驟 1
A=[(−3,0),(−2,2),(−1,−1)],B=[(1,0),(2,2),(3,−1)]。
A三點
B三點
- 示例
- 兩個三角形
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using boost::multiprecision::cpp_int;
using ll = long long;
#include <boost/rational.hpp>
using Q = boost::rational<cpp_int>;
Q rat(cpp_int a, cpp_int b) {
if (b < 0) {
a = -a;
b = -b;
}
return Q(a, b);
}
cpp_int abig(cpp_int a) {
return a < 0 ? -a : a;
}
cpp_int ten(int n) {
cpp_int a = 1;
while (n--)
a *= 10;
return a;
}
Q dec(string s) {
auto slash = s.find('/');
if (slash != string::npos)
return Q(cpp_int(s.substr(0, slash)), cpp_int(s.substr(slash + 1)));
bool neg = !s.empty() && s[0] == '-';
if (s[0] == '-' || s[0] == '+')
s.erase(s.begin());
auto p = s.find('.');
int k = p == string::npos ? 0 : s.size() - p - 1;
if (p != string::npos)
s.erase(p, 1);
size_t z = s.find_first_not_of('0');
cpp_int a = z == string::npos ? cpp_int(0) : cpp_int(s.substr(z));
return Q(neg ? -a : a, ten(k));
}
string fixedq(Q q, int digits) {
cpp_int a = q.numerator(), b = q.denominator(), scale = ten(digits);
bool neg = a < 0;
a = abig(a);
cpp_int k = (2 * a * scale + b) / (2 * b);
string s = k.convert_to<string>();
if (digits) {
if (s.size() <= digits)
s = string(digits + 1 - s.size(), '0') + s;
s.insert(s.size() - digits, ".");
}
return (neg && k != 0 ? "-" : "") + s;
}
string finiteq(Q q) {
cpp_int a = q.numerator(), b = q.denominator();
bool neg = a < 0;
a = abig(a);
cpp_int whole = a / b, r = a % b;
string s = whole.convert_to<string>();
if (r != 0) {
s += '.';
while (r != 0) {
r *= 10;
cpp_int z = r / b;
s += char('0' + z.convert_to<int>());
r %= b;
}
}
return (neg ? "-" : "") + s;
}
struct Pt {
cpp_int x, y;
bool operator<(const Pt &b) const {
return x != b.x ? x < b.x : y < b.y;
}
bool operator==(const Pt &b) const {
return x == b.x && y == b.y;
}
};
cpp_int cross(Pt a, Pt b, Pt c) {
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}
vector<Pt> hull(vector<Pt> a) {
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
if (a.size() < 3)
return a;
vector<Pt> h;
for (Pt p : a) {
while (h.size() > 1 && cross(h[h.size() - 2], h.back(), p) <= 0)
h.pop_back();
h.push_back(p);
}
size_t l = h.size();
for (int i = (int)a.size() - 2; i >= 0; i--) {
Pt p = a[i];
while (h.size() > l && cross(h[h.size() - 2], h.back(), p) <= 0)
h.pop_back();
h.push_back(p);
}
h.pop_back();
return h;
}
int main() {
int n;
cin >> n;
vector<Pt> p(n);
for (auto &q : p)
cin >> q.x >> q.y;
sort(p.begin(), p.end());
vector<Pt> A(p.begin(), p.begin() + n / 2), B(p.begin() + n / 2, p.end());
A = hull(A);
B = hull(B);
for (auto &q : A) {
q.x = -q.x;
q.y = -q.y;
}
auto rotateLow = [](vector<Pt> &v) {
int k = 0;
for (int i = 1; i < (int)v.size(); i++)
if (v[i].y < v[k].y || (v[i].y == v[k].y && v[i].x < v[k].x))
k = i;
rotate(v.begin(), v.begin() + k, v.end());
};
rotateLow(A);
rotateLow(B);
int na = A.size(), nb = B.size(), i = 0, j = 0, sa = 0, sb = 0;
Q low, high;
bool first = true;
array<cpp_int, 3> l1, l2;
auto eval = [&]() {
cpp_int dx = A[i].x + B[j].x, dy = A[i].y + B[j].y;
Q slope(dy, dx);
cpp_int ax = -A[i].x, ay = -A[i].y;
array<cpp_int, 3> line = {dy, -dx, -dy * ax + dx * ay};
if (first || slope < low) {
low = slope;
l1 = line;
}
if (first || slope > high) {
high = slope;
l2 = line;
}
first = false;
};
eval();
while (sa < na || sb < nb) {
bool aa = false, bb = false;
if (sa == na)
bb = true;
else if (sb == nb)
aa = true;
else {
Pt u{A[(i + 1) % na].x - A[i].x, A[(i + 1) % na].y - A[i].y},
v{B[(j + 1) % nb].x - B[j].x, B[(j + 1) % nb].y - B[j].y};
cpp_int c = u.x * v.y - u.y * v.x;
aa = c >= 0;
bb = c <= 0;
}
if (aa) {
i = (i + 1) % na;
sa++;
}
if (bb) {
j = (j + 1) % nb;
sb++;
}
eval();
}
auto [a, b, c] = l1;
auto [d, e, f] = l2;
cpp_int det = a * e - b * d;
cout << fixedq(rat(b * f - c * e, det), 2) << ' '
<< fixedq(rat(c * d - a * f, det), 2) << '\n';
}
先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。