[ABC301G] Worst Picture
本题考察你高中数学基本功。
首先,你肯定能找到一个角度让
然后,可能存在多个人共线的情况,此时在这条直线上拍照只能拍到这些共线的人中的一个。我们可以枚举任意两点
还有一种情况,答案所在的点
两条直线有交点的充分必要条件是四个点共面。记
解决了这个问题,就可以直接上公式了。
其中
推导:
设
\boldsymbol p=\boldsymbol a+t\boldsymbol{v_1}=\boldsymbol c+s\boldsymbol{v_2} 那么
t\boldsymbol{v_1}-s\boldsymbol{v_2}=\boldsymbol c-\boldsymbol a = \boldsymbol w 想办法干掉
s 。可以利用叉乘的性质。两边同时叉上\boldsymbol{v_2} :t(\boldsymbol{v_1}\times\boldsymbol{v_2})-s(\boldsymbol{v_2}\times\boldsymbol{v_2})=\boldsymbol w \times \boldsymbol{v_2} t(\boldsymbol{v_1}\times\boldsymbol{v_2})=\boldsymbol w \times \boldsymbol{v_2} t(\boldsymbol{v_1}\times\boldsymbol{v_2})^2=(\boldsymbol w \times \boldsymbol{v_2})\cdot(\boldsymbol{v_1}\times\boldsymbol{v_2}) t=\dfrac{(\boldsymbol w \times \boldsymbol{v_2})\cdot(\boldsymbol{v_1}\times\boldsymbol{v_2})}{(\boldsymbol{v_1}\times\boldsymbol{v_2})^2}
还需要注意
求出了
考虑到去重,浮点精度问题会很麻烦,我选择使用有理数表示。约分太慢了,而且本题坐标范围不大,本身也用不到几次乘法,开 int128 足够了,不需要约分。最终时间复杂度
#include <cmath>
#include <cstddef>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <set>
#include <utility>
#include <vector>
using namespace std;
istream& fin = cin;
ostream& fout = cout;
using ui = unsigned int;
using uli = unsigned long long int;
using li = long long int;
using lli = __int128_t;
struct Fraction {
lli a;
lli b;
void normalize() {
// if (b == 0) throw std::invalid_argument("Denominator cannot be zero");
// if (a == 0) {
// b = 1;
// return;
// }
// if (b < 0) a = -a, b = -b;
// li d = gcd(a, b);
// a /= d, b /= d;
}
Fraction(li num = 0, li den = 1): a(num), b(den) { normalize(); }
Fraction operator-() const { return Fraction(-a, b); }
Fraction operator+(const Fraction& other) const {
li num = a * other.b + other.a * b;
li den = b * other.b;
return Fraction(num, den);
}
Fraction operator-(const Fraction& other) const { return *this + (-other); }
Fraction operator*(const Fraction& other) const {
li num = a * other.a;
li den = b * other.b;
return Fraction(num, den);
}
Fraction operator/(const Fraction& other) const {
if (other.a == 0) { throw std::runtime_error("Division by zero"); }
return *this * Fraction(other.b, other.a);
}
Fraction& operator+=(const Fraction& other) {
*this = *this + other;
return *this;
}
Fraction& operator-=(const Fraction& other) {
*this = *this - other;
return *this;
}
Fraction& operator*=(const Fraction& other) {
*this = *this * other;
return *this;
}
Fraction& operator/=(const Fraction& other) {
*this = *this / other;
return *this;
}
friend bool operator==(const Fraction& x, const Fraction& y) {
return x.a * y.b == y.a * x.b;
}
friend bool operator!=(const Fraction& x, const Fraction& y) {
return !(x == y);
}
friend bool operator<(const Fraction& x, const Fraction& y) {
return x.a * y.b < y.a * x.b;
}
friend bool operator>(const Fraction& x, const Fraction& y) { return y < x; }
friend bool operator<=(const Fraction& x, const Fraction& y) {
return !(x > y);
}
friend bool operator>=(const Fraction& x, const Fraction& y) {
return !(x < y);
}
double toDouble() const { return double(a) / b; }
};
struct Vector3D {
Fraction x, y, z;
Vector3D& operator+=(Vector3D const& that) {
x += that.x, y += that.y, z += that.z;
return *this;
}
friend Vector3D operator+(Vector3D a, Vector3D const& b) { return a += b; }
Vector3D& operator-=(Vector3D const& that) {
x -= that.x, y -= that.y, z -= that.z;
return *this;
}
friend Vector3D operator-(Vector3D a, Vector3D const& b) { return a -= b; }
Vector3D& operator*=(Fraction const& k) {
x *= k, y *= k, z *= k;
return *this;
}
friend Vector3D operator*(Vector3D a, Fraction const& k) { return a *= k; }
friend Vector3D operator*(Fraction const& k, Vector3D a) { return a *= k; }
friend Fraction operator*(Vector3D const& a, Vector3D const& b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
}
friend Vector3D operator^(Vector3D const& a, Vector3D const& b) {
return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x};
}
Vector3D operator-() const { return {-x, -y, -z}; }
friend bool operator==(Vector3D const& a, Vector3D const& b) {
return a.x == b.x && a.y == b.y && a.z == b.z;
}
friend bool operator!=(Vector3D const& a, Vector3D const& b) {
return !(a == b);
}
};
int main(void) {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
size_t n;
fin >> n;
vector<Vector3D> a(n);
for (Vector3D& i : a) {
int x, y, z;
fin >> x >> y >> z;
i.x = x, i.y = y, i.z = z;
};
ui ans = n;
for (auto pa : a)
for (auto pb : a)
if (pa != pb) {
auto v1 = pb - pa;
if (v1.x == 0) continue;
ui c = n;
for (auto pi : a)
if (pi != pa) {
auto v2 = pi - pa;
auto vc = v1 ^ v2;
if (vc.x == 0 && vc.y == 0 && vc.z == 0) --c;
}
ans = min(ans, c);
}
for (auto pa : a)
for (auto pb : a)
for (auto pc : a)
for (auto pd : a)
if (pa != pb && pb != pc && pc != pd) {
// 计算两直线交点
auto v1 = pb - pa, v2 = pd - pc, w = pc - pa;
if ((v1 ^ w) * v2 != 0) continue; // 不共面
auto r = (v1 ^ v2) * (v1 ^ v2);
if (r == 0) continue; // 平行
auto t = (w ^ v2) * (v1 ^ v2) / r;
auto ps = pa + t * v1;
if (ps.x >= 0) continue;
set<pair<Fraction, Fraction>> ts;
for (auto pi : a) {
auto r = pi - ps;
ts.emplace(r.y / r.x, r.z / r.x);
}
ans = min(ans, (ui)ts.size());
}
fout << ans;
return 0;
}