跳到正文

题解

[ABC301G] Worst Picture

MrPython约 14 分钟
原文
文章信息与版本

文章信息

发布
2025年6月26日
收录
2025年12月12日
更新
2026年10月14日
篇幅
约 14 分钟
获赞
1

版本

  1. v1当前

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

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

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

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

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

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

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

其中

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

推导:

设

p=a+tv1=c+sv2\boldsymbol p=\boldsymbol a+t\boldsymbol{v_1}=\boldsymbol c+s\boldsymbol{v_2}

那么

tv1−sv2=c−a=wt\boldsymbol{v_1}-s\boldsymbol{v_2}=\boldsymbol c-\boldsymbol a = \boldsymbol w

想办法干掉 ss。可以利用叉乘的性质。两边同时叉上 v2\boldsymbol{v_2}:

t(v1×v2)−s(v2×v2)=w×v2t(\boldsymbol{v_1}\times\boldsymbol{v_2})-s(\boldsymbol{v_2}\times\boldsymbol{v_2})=\boldsymbol w \times \boldsymbol{v_2} t(v1×v2)=w×v2t(\boldsymbol{v_1}\times\boldsymbol{v_2})=\boldsymbol w \times \boldsymbol{v_2} t(v1×v2)2=(w×v2)⋅(v1×v2)t(\boldsymbol{v_1}\times\boldsymbol{v_2})^2=(\boldsymbol w \times \boldsymbol{v_2})\cdot(\boldsymbol{v_1}\times\boldsymbol{v_2}) t=(w×v2)⋅(v1×v2)(v1×v2)2t=\dfrac{(\boldsymbol w \times \boldsymbol{v_2})\cdot(\boldsymbol{v_1}\times\boldsymbol{v_2})}{(\boldsymbol{v_1}\times\boldsymbol{v_2})^2}

还需要注意 PP 的 xx 坐标小于 00。

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

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

C++
#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;
}
原文
存档评论2 条抓取于