题解:P14914 「QFOI R3」航线交汇

· · 题解

思路

定义小 R 坐的飞机起点为 s_0,终点为 t_0,高度为 h_0。设相遇时间为总时间的比例 p(0 \le p \le 1),则:

则有方程:

\begin{aligned} s_0+p(t_0-s_0)&=s_i+p(t_i-s_i)\\ s_0-s_i&=p(t_i-s_i-t_0+s_0)\\ p&=\frac{s_0-s_i}{(t_i-s_i)-(t_0-s_0)} \end{aligned}

先求得 p,再求出小 R 的飞机位置即可。

Code

#include<bits/stdc++.h>
using namespace std;
int n,d,s0,t0,h0;
int main(){
    cin>>n>>d>>s0>>t0>>h0;
    while(n--){
        int s,t,h;cin>>s>>t>>h;
        if(abs(h0-h)>d){cout<<"No\n";continue;}//检查高度差
        int v=(t-s)-(t0-s0);//计算分母
        if(!v){//排除分母为 $0$
            if(s==s0) cout<<"Always\n";//速度相同
            else cout<<"No\n";
        }else{
            long double p=(s0-s)*1.0/v;//计算相遇时间比例 p
            if(p>=0&&p<=1){//检查 p 是否在 [0,1] 范围内
                long double pos=s0+p*(t0-s0);//计算位置
                printf("%.12Lf\n",pos);
            }else cout<<"No\n";
        }
    }
}