跳到正文

题解

P9784 题解

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

文章信息

发布
2023年12月27日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 2 分钟
获赞
5
题目
P9784

版本

  1. v1当前

想知道每辆车在所有路段中最高被罚款的金额至少是多少,你只需要知道每辆车最大超速是多少。

如果让开车的时间最短,我们就要让罚款物尽其用。具体地讲,如果我们的超速在第 pp 档中,就需要将每一段的速度设为 vi+apv_i+a_p,这样我们计算总时间。

我们要寻找使时间 ≤t−s\leq t-s 的最小超速,这个问题是可以二分的。于是二分解决即可,时间复杂度 O(qnlog⁡m)O(qn\log m)。

具体细节看代码:

C++
#include<iostream>
#include<algorithm>
using ll=long long;
const int sz=1e5+10;
int n,m,q;
ll v[15],l[15],a[sz],f[sz];
bool check(ll dv,int lim){
    double tot=0;
    for(int i=1;i<=n;i++)
        tot+=1.*l[i]/(v[i]+dv);
    return tot<=lim;
}
int query(int tot){
    int l=0,r=m-1;
    while(l<r){
        int mid=l+r>>1;
        if(check(a[mid],tot))r=mid;
        else l=mid+1;
    }
    if(!check(a[l],tot))l++;
    return f[l];
}
int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cin>>n;
    for(int i=1;i<=n;i++)std::cin>>v[i];
    for(int i=1;i<=n;i++)std::cin>>l[i];
    std::cin>>m;
    for(int i=1;i<m;i++)std::cin>>a[i];
    for(int i=1;i<=m;i++)std::cin>>f[i];
    std::cin>>q;
    while(q--){
        int s,t;
        std::cin>>s>>t;
        std::cout<<query(t-s)<<"\n";
    }
    return 0;
}
原文