跳到正文

题解

P3218 [HNOI]赛车游戏题解

weior约 4 分钟
原文
文章信息与版本

文章信息

发布
2021年10月8日
收录
2026年2月6日
更新
2026年11月12日
篇幅
约 4 分钟
题目
P3218

版本

  1. v2当前
  2. v1

辛苦审核大大Orz

许多题解对 vv 为什么全程相等最优讲得云里雾里就自己大致证了一下

基本只有对 vv 全程相等的感性证明,算法还是得看其他大佬的代码。

先考虑不取 maxmax 的情况,每段路的 a×v+b×sa \times v + b \times s 中 b×sb \times s 是固定的,而 aa 是常量,所以当我们取 v=(f−∑i=1nb×si)av = \frac {(f - \sum_{i=1}^n b \times s_i )}{a} 时能够刚好将油耗完且跑到终点。

所以当不取 maxmax 时,我们让 vv 保持全程相同是一定最优的。

现在再来考虑取 maxmax 的下坡部分(上坡和平路肯定不会取到 00 )。

由于下坡时耗油量为 max⁡(0,a×v+b×s)\max(0,a \times v + b \times s) 所以当 a×v+b×sa \times v + b \times s 非负时,对答案是没有影响的,只有当它为负时,由于与 00 取 maxmax ,所以会使我们的耗油量增加。

所以我们只有将非下坡路的速度降低,以此来平衡多出来的油量。

由于我们证明过让 vv 全程相同是最优的,所以我们可以二分 vv 来判断是否合法。

最后算时间时还要注意在下坡时如果我们的速度比 −b×sa\frac {- b \times s} {a} 小,则我们可以将速度提高到 min⁡(vmax,−b×sa)\min(vmax,\frac {- b \times s} {a}) 。 反正不耗油那就快乐飙车

附上蒟蒻代码Orz

C++
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<string>
#include<math.h>
#include<queue>
#include<map>
#define LL long long
#define MAXN 1000005
using namespace std;
const double eps=1e-15;

int T,R;
double A,B,vmax,F;
double X[10002],Y[10002],k[10002],len[10002];

bool check(double mid) {
    double lf=0;
    for(int i=1; i<=R; ++i) {
        lf+=max(0.0,mid*A+B*k[i])*len[i];
        if(lf-F>eps)return 0;
    }
    if(lf-F<=eps)return 1;
    return 0;
}

double cal(double x) {
    double lf=0;
    if(x<=eps)return 0;
    for(int i=1; i<=R; ++i) {
        double tmp=A*x+B*k[i];
        if(tmp<=eps){
            double v=min(vmax,-B*k[i]/A);
            lf+=len[i]/v;
        }else lf+=len[i]/x;
    }
    return lf;
}

void work() {
    double l=0,r=vmax;
    int TYP=0;
    while(TYP<=100) {
        double mid=(l+r)/2.0;
        if(check(mid))l=mid;
        else r=mid;
        ++TYP;
    }
    double ans=cal(l);
    if(ans<=eps)printf("IMPOSSIBLE\n");
    else printf("%.5lf\n",ans);
}

int main() {
    scanf("%d",&T);
    while(T--) {
        scanf("%lf%lf%lf%lf%d",&A,&B,&vmax,&F,&R);
        for(int i=1; i<=R; ++i) {
            scanf("%lf%lf",&X[i],&Y[i]);
            X[i]/=1000.0,Y[i]/=1000.0;
            k[i]=Y[i]/X[i];
            len[i]=sqrt(X[i]*X[i]+Y[i]*Y[i]);
        }
        work();
    }
    return 0;
}
原文