[ABC301G] Worst Picture

· · 题解

本题考察你高中数学基本功。

首先,你肯定能找到一个角度让 n 个人都能出镜。所以答案最差为 n。

然后,可能存在多个人共线的情况,此时在这条直线上拍照只能拍到这些共线的人中的一个。我们可以枚举任意两点 A 和 B(记两点所对应的向量为 \boldsymbol a 和 \boldsymbol b)所连成的直线。然后,求出这条直线的方向向量 \boldsymbol {v_1}=\boldsymbol b-\boldsymbol a,再枚举除 \boldsymbol a 以外的所有点 \boldsymbol x,判断 \boldsymbol x - \boldsymbol a 是否共线(叉乘为 \boldsymbol 0)即可得知 \boldsymbol x 是否在直线 AB 上。注意,按照题目要求,这条直线必须要进入 x 为负的区域,即其方向向量的 x 分量不得为 0。

还有一种情况,答案所在的点 P(记其所对应向量为 \boldsymbol p)在四个点 A,B,C,D(记其所对应向量分别为 \boldsymbol a,\boldsymbol b,\boldsymbol c,\boldsymbol d)所组成的两条直线(AB 和 CD)的交点上。枚举四个点,求出这两条直线的交点即可找到 S。

两条直线有交点的充分必要条件是四个点共面。记 \boldsymbol{v_1}=\boldsymbol b - \boldsymbol a, \boldsymbol{v_2}=\boldsymbol d-\boldsymbol c,\boldsymbol w=\boldsymbol c-\boldsymbol a。则 \boldsymbol{v_1}\times\boldsymbol w 即为 \text{平面 }ABC 的法向量。若 D 也在 \text{平面 }ABC 中,则应有 CD\perp \text{平面 }ABC,也就是 \boldsymbol{v_2}\perp \left(\boldsymbol{v_1}\times\boldsymbol w\right),即 \left(\boldsymbol{v_1}\times\boldsymbol w\right)\cdot\boldsymbol{v_2} = 0。若该条件成立,则两直线相离,跳过。两直线也不能平行。判断 \boldsymbol{v_1}\times\boldsymbol{v_2}=\boldsymbol 0 即可。

解决了这个问题,就可以直接上公式了。

\boldsymbol p=\boldsymbol a+t\boldsymbol{v_1}

其中

t=\dfrac{(w \times \boldsymbol{v_2})\cdot(\boldsymbol{v_1}\times\boldsymbol{v_2})}{(\boldsymbol{v_1}\times\boldsymbol{v_2})^2}

推导:

设

\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}

还需要注意 P 的 x 坐标小于 0。

求出了 P 的坐标,就可以统计在该点的答案了。可以遍历所有的点,记录下该点到 P 的方向向量,最后去重。方向相同的向量有许多个,需要将其归到同一个去。注意到 x 方向的分量一定不为 0,将向量除以 x 方向的分量就可以了。

考虑到去重,浮点精度问题会很麻烦,我选择使用有理数表示。约分太慢了,而且本题坐标范围不大,本身也用不到几次乘法,开 int128 足够了,不需要约分。最终时间复杂度 O(n^5 \log n)。

#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;
}