跳到正文

题解

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

YuKeWang约 7 分钟
原文
文章信息与版本

文章信息

收录
2026年1月3日
更新
2026年1月10日
篇幅
约 7 分钟
题目
P14914

版本

  1. v2当前
  2. v1

P14914 题解

题目传送门

先上代码(加注释)知道你们先看这个

C++
#include<bits/stdc++.h>
using namespace std;
int n;
long long d;
struct plane
{
	double be,en,v;
	long long hi;
	void read()
	{
		cin>>be>>en>>hi;
		v=abs(en-be);
	}
}p[100005];
int main()
{
	ios::sync_with_stdio(0);
	cin>>n>>d;
	for(int i = 1;i <= n+1;i++)
		p[i].read();
	double BE=p[1].be;
	double EN=p[1].en;
	double V = p[1].v;
	long long HI=p[1].hi;
	for(int i = 2;i <= n+1;i++)
	{
        if(abs(p[i].hi-HI)>d||min(BE,EN)>max(p[i].be,p[i].en)||max(EN,BE)<min(p[i].en,p[i].be))
    	    cout<<"No\n";
	    else if(BE<=EN&&p[i].be<=p[i].en)//追及
	    {
	    	if(BE==p[i].be&&EN==p[i].en)//一样的路线
	    		cout<<"Always\n";
	    	else if(BE==p[i].be)//在起点
	    		cout<<BE<<endl;
	    	else if(EN==p[i].en)//在终点
	    		cout<<EN<<endl;
            else
            {
    	    	if(BE>p[i].be)
    	    	{
    	    		double cha=BE-p[i].be;
    	    		if(p[i].v<=V)//追不上
    	    			cout<<"No\n";
                    else
                    {
        	    		double t = cha/(p[i].v-V);
        	    		if(t>1)//也是追不上
        	    			cout<<"No\n";
        	    		else // 能追上
        	    			cout<<BE+t*V<<endl;
                    }
    	    	}
    	    	else
    	    	{
    	    		double cha=p[i].be-BE;
    	    		if(p[i].v>=V)//追不上
    	    			cout<<"No\n";
                    else
                    {
        	    		double t = cha/(V-p[i].v);
        	    		if(t>1)//也是追不上
        	    			cout<<"No\n";
        	    		else // 能追上
        	    			cout<<BE+t*V<<endl;
                    }
        	    }
            }
	    }
		else if(BE<=EN&&p[i].be>p[i].en) //相遇不可能全程看见
	    {
			double he = p[i].be-BE;
			double t = he/(p[i].v+V);
			if(t>1)//没有相遇
				cout<<"No\n";
			else
				cout<<BE+t*V<<endl;
	    }
	    else if(BE>EN&&p[i].be<=p[i].en) //相遇不可能全程看见
		{
			double he = BE-p[i].be;
			double t = he/(p[i].v+V);
			if(t>1)//没有相遇
				cout<<"No\n";
			else
				cout<<BE-t*V<<endl;
		}
		else if(BE>EN&&p[i].be>p[i].en)//追及
		{
			if(BE==p[i].be&&EN==p[i].en)//一样的路线
			    cout<<"Always\n";
			else if(BE==p[i].be)//在起点
			    cout<<BE<<endl;
			else if(EN==p[i].en)//在终点
			    cout<<EN<<endl;
			else
            {
                if(BE>p[i].be)
    			{
    			    double cha=BE-p[i].be;
    			    if(p[i].v>V)//追不上
    			    	cout<<"No\n";
                    else
                    {
        			    double t = cha/(V-p[i].v);
        			    if(t>1)//也是追不上
        			    	cout<<"No\n";
        			    else // 能追上
        			    	cout<<BE-t*V<<endl;
                    }
    			}
    			else
    			{
    			    double cha=p[i].be-BE;
    			    if(p[i].v<V)//追不上
    			    	cout<<"No\n";
                    else
                    {
        			    double t = cha/(p[i].v-V);
        			    if(t>1)//也是追不上
        			    	cout<<"No\n";
        			    else // 能追上
        			    	cout<<BE-t*V<<endl;
                    }
    			}
            }
		}
	}
	return 0;
}

AC记录

思路

  • 用结构体来存每一条航线

  • 设总时间为1,如果算出来的时间大于1直接输出”No”

  • 不难发现,两架飞机只有整条路经完全相同才能全程都看见

  • 如果 两架飞机航线完全不相交,直接输出”No”

  • 速度相当于路程,记得加绝对值

  • 然后枚举2~n+1每一个飞机进行分类讨论

分类讨论

  1. 追及问题,1号飞机为正方向

  2. 相遇问题,1号飞机为正方向

  3. 相遇问题,1号飞机为反方向

  4. 追及问题,1号飞机为反方向


代码有点长,大佬不喜勿喷

本蒟蒻第一篇题解,希望管理员通过

有疑问评论区留言

原文