解題思路
求隨機三點外接圓平均覆蓋的房屋數。
每個三角形先覆蓋自身3點。再看任意四點:四點凸位置時恰有2個三角形的外接圓包含第四點;若一點在其餘三角形內,恰有1個。無三共線、四共圓排除了邊界歧義。
令I為含內點的四點組數,全部額外覆蓋貢獻為2C(n,4)−I,平均即3+(2C(n,4)−I)/C(n,3)。
對每個點為中心,將其他向量極角排序;不包含中心的三角形恰落在某開半平面,用雙指標計每個向量之後小於180度的k個向量並累加C(k,2)。以C(n−1,3)扣除此數即包含中心的三角形數,加總得到I。
時間 O(n² log n)次任意精度叉積與比較,空間 O(n)個向量;另計座標位數。
Step-by-step animation
四點凸位置與一內點情況的平均比較
步驟 1 / 3
步驟 1
n=4,若四點皆凸位置,四個三角形共有2次額外覆蓋。
C4,3=4
額外2
- 步驟
- 1
#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;
}
cpp_int choose(ll n, int k) {
if (n < k)
return 0;
cpp_int a = 1;
for (int i = 1; i <= k; i++)
a = a * (n - i + 1) / i;
return a;
}
int main() {
int n;
cin >> n;
vector<Pt> p(n);
for (auto &a : p)
cin >> a.x >> a.y;
cpp_int inside = 0;
auto cp = [](Pt a, Pt b) { return cpp_int(a.x * b.y - a.y * b.x); };
auto half = [](Pt a) { return a.y > 0 || (a.y == 0 && a.x > 0) ? 0 : 1; };
for (int c = 0; c < n; c++) {
vector<Pt> v;
for (int j = 0; j < n; j++)
if (j != c)
v.push_back({p[j].x - p[c].x, p[j].y - p[c].y});
sort(v.begin(), v.end(), [&](Pt a, Pt b) {
return half(a) != half(b) ? half(a) < half(b) : cp(a, b) > 0;
});
int m = v.size();
auto copy = v;
v.insert(v.end(), copy.begin(), copy.end());
cpp_int outside = 0;
int r = 1;
for (int l = 0; l < m; l++) {
r = max(r, l + 1);
while (r < l + m && cp(v[l], v[r]) > 0)
r++;
outside += choose(r - l - 1, 2);
}
inside += choose(m, 3) - outside;
}
Q avg = Q(3) + Q(2 * choose(n, 4) - inside, choose(n, 3));
cout << fixedq(avg, 6) << '\n';
}
先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。