跳到正文

题解

题解:P16226 [蓝桥杯 2026 省 A] 拦截程序

_ZXY_约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年4月11日
收录
2026年4月12日
更新
2026年11月14日
篇幅
约 2 分钟
题目
P16226

版本

  1. v2当前
  2. v1

我们发现,当 PP 为 PAP_A 和 PBP_B 的中点时,∣PA−PB+1∣2\frac{|P_A-P_B+1|}{2} 一定是最优距离。

证明

若 PP 不为 PAP_A 和 PBP_B 的中点,则距离两个点个点的距离分别为 ∣PA−PB+1∣2−x\frac{|P_A-P_B+1|}{2}-x 和 ∣PA−PB+1∣2+x\frac{|P_A-P_B+1|}{2}+x,取 max⁡\max 一定是不如去中点优的。

所以我们可以写出代码:

C++
#include<bits/stdc++.h>
#define int __int128
using namespace std;
int C,l,v,t;
inline int read(){
    int f=0;char ch=getchar();
    while(ch<48||ch>57)ch=getchar();
    while(ch>47&&ch<58)f=(f<<3)+(f<<1)+(ch^48),ch=getchar();
    return f;
}
void print(int a){
    if(a>9)print(a/10);
    putchar(a%10+'0');
}
signed main(){
    C=read();
    while(C--){
        int l,v,t;
        l=read();
        v=read();
        t=read();
        int L=v*t,R=l-v*t;
        int ans=(max(R,L)-min(L,R)+1)/2;
        if(ans<0)ans=-ans;
        print(ans);
        cout<<endl;
    }
}

这道题唯一的坑点就在于有可能 PB<PAP_B<P_A。

原文