跳到正文

题解

题解:UVA10794 The Deadly Olympic Returns!!!

_Luk_约 5 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月29日
篇幅
约 5 分钟
获赞
1
题目
UVA10794

版本

  1. v1当前

题解

OMG 审核大大我要疯了,这是我自己写哒!!!!!

题目大意:

建议去搜一下翻译。

新的国际奥委会(IOC)对比赛项目进行了创新,引入类似古老摔跤游戏的空战形式,参赛双方使用本国空军提供的飞机携带导弹进行对抗。比赛中双方同时发射导弹,一方发射进攻导弹,另一方发射拦截导弹,若双方都未被击中则比赛继续。

主要解法:

这道题的核心是计算两个导弹在飞行过程中的最小距离。解题的关键在于根据给定的初始位置和经过特定时间后的位置,确定导弹的运动轨迹(直线),然后找出两条直线间的最小距离。

思路:

用一个 Vector3D 结构体,重载减法运算符,实现点积、叉积和模长的计算方法。

  1. 把导弹的运动当作匀速直线运动,算出导弹的速度向量。
  2. 距离函数,用于计算在任意时刻 tt 两枚导弹之间的距离。
  3. 最小距离计算,使用三分法在时间区间 [0,time][0, time] 内找出距离函数的最小值。
  • 时间复杂度:O(T)O(T)。
  • 空间复杂度:O(1)O(1)。

code:code:

C++
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const double N = 1e-9;

struct f1 { // 三维向量结构体
    double x, y, z;
    f1(double _x = 0, double _y = 0, double _z = 0) : x(_x), y(_y), z(_z) {}

    f1 operator-(const f1& other) const { // 向量减法
        return f1(x - other.x, y - other.y, z - other.z);
    }

    f1 operator*(double t) const { // 向量乘以标量
        return f1(x * t, y * t, z * t);
    }

    f1 operator+(const f1& other) const { // 向量加法
        return f1(x + other.x, y + other.y, z + other.z);
    }

    double length() const { // 向量的模
        return sqrt(x * x + y * y + z * z);
    }
};

// 计算在时刻 t 两枚导弹之间的距离
double f2(const f1& p1, const f1& v1, const f1& p2, const f1& v2, double t) {
    f1 pos1 = p1 + v1 * t;
    f1 pos2 = p2 + v2 * t;
    return (pos1 - pos2).length();
}

// 三分法求最小值
double f3(const f1& p1, const f1& v1, const f1& p2, const f1& v2, double t) {
    double l = 0, r = t;
    while (r - l > N) {
        double m1 = l + (r - l) / 3;
        double m2 = r - (r - l) / 3;
        double d1 = f2(p1, v1, p2, v2, m1);
        double d2 = f2(p1, v1, p2, v2, m2);
        if (d1 < d2) {
            r = m2;
        } else {
            l = m1;
        }
    }
    return f2(p1, v1, p2, v2, (l + r) / 2);
}

int main () {
    int T; cin >> T;
    for (int i = 1; i <= T; ++i) {
        int t; cin >> t;
        int x1, y1, z1, x2, y2, z2; cin >> x1 >> y1 >> z1 >> x2 >> y2 >> z2;
        f1 p1(x1, y1, z1);
        f1 end1(x2, y2, z2);
        f1 v1 = (end1 - p1) * (1.0 / t);

        cin >> x1 >> y1 >> z1 >> x2 >> y2 >> z2;
        f1 p2(x1, y1, z1);
        f1 end2(x2, y2, z2);
        f1 v2 = (end2 - p2) * (1.0 / t);

        double minn = f3(p1, v1, p2, v2, t);
        cout << "Case " << i << ": " << fixed << setprecision(4) << minn << '\n';
    }
    return 0;
}
原文