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

· · 题解

我们发现,当 P 为 P_A 和 P_B 的中点时,\frac{|P_A-P_B+1|}{2} 一定是最优距离。
::::info[证明] 若 P 不为 P_A 和 P_B 的中点,则距离两个点个点的距离分别为 \frac{|P_A-P_B+1|}{2}-x 和 \frac{|P_A-P_B+1|}{2}+x,取 \max 一定是不如去中点优的。 :::: 所以我们可以写出代码:

#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;
    }
}

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