解題思路
求過原點等分凸多邊形面積的斜率。
先把任意輸入順序的邊界點建成凸包。對有理斜率s,以半平面y≤sx精確裁切多邊形,鞋帶公式得到下方面積。
因全部x>0,下方面積對s單調遞增,從0到全面積,因此唯一等面積斜率可用面積比較定位。
不近似求根,而先二分找floor(真斜率×10000),再在正中的half-up捨入分界做一次精確面積比較。分界恰等面積時,正數向上、負數向下,完整滿足指定捨入。
時間 O(n log n+ n log R)次有理數幾何操作,空間 O(n);R為斜率界乘10000,座標限制下R≤10^10,另計位數。
Step-by-step animation
在捨入分界精確比較切割面積逐步追蹤
步驟 1 / 3
步驟 1
示例三角形(1,3)、(3,1)、(4,4),關於y=x對稱。
凸包三頂點
- 步驟
- 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;
}
cpp_int isq(cpp_int n) {
if (n <= 0)
return 0;
cpp_int x = cpp_int(1) << ((boost::multiprecision::msb(n) + 2) / 2);
while (true) {
cpp_int y = (x + n / x) / 2;
if (y >= x)
return x;
x = y;
}
}
string scaled(cpp_int k, int d) {
bool neg = k < 0;
if (neg)
k = -k;
string s = k.convert_to<string>();
if (d) {
if (s.size() <= d)
s = string(d + 1 - s.size(), '0') + s;
s.insert(s.size() - d, ".");
}
return (neg && k != 0 ? "-" : "") + 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> a(n);
for (auto &p : a)
cin >> p.x >> p.y;
a = hull(a);
using P = pair<Q, Q>;
vector<P> v;
for (auto p : a)
v.push_back({Q(p.x), Q(p.y)});
auto area2 = [](vector<P> p) {
Q s(0);
for (int i = 0; i < (int)p.size(); i++)
s += p[i].first * p[(i + 1) % p.size()].second -
p[i].second * p[(i + 1) % p.size()].first;
return s < 0 ? -s : s;
};
Q full = area2(v);
auto compare = [&](Q slope) {
vector<P> out;
for (int i = 0; i < (int)v.size(); i++) {
auto p = v[i], q = v[(i + 1) % v.size()];
Q f = p.second - slope * p.first, g = q.second - slope * q.first;
if (f <= 0)
out.push_back(p);
if ((f < 0 && g > 0) || (f > 0 && g < 0)) {
Q t = f / (f - g);
out.push_back({p.first + t * (q.first - p.first),
p.second + t * (q.second - p.second)});
}
}
Q d = 2 * area2(out) - full;
return d < 0 ? -1 : d > 0 ? 1 : 0;
};
ll lo = -10000000000LL, hi = 10000000000LL;
while (lo + 1 < hi) {
ll mid = lo + (hi - lo) / 2;
if (compare(Q(mid, 10000)) <= 0)
lo = mid;
else
hi = mid;
}
int cmp = compare(Q(2 * lo + 1, 20000));
ll k = lo;
if (cmp < 0 || (cmp == 0 && 2 * lo + 1 > 0))
k++;
cout << scaled(cpp_int(k), 4) << '\n';
}
先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。