P9784 [ROIR 2020 Day1] 超速 题解

· · 题解

题目传送门:link。

思路:一看最大值最小,二分直接秒。二分出在哪个罚款段,因为答案是取最高罚款,所以在每段路都要跑到上限,速度为 v_i+a_{mid} 米每秒。车从 s 时间来到 t 时间去,花费 t-s 时间,只需要将二分出的答案与其比较即可。

二分好难调啊。

code

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"

const int N=100005,M=5005,inf=1e18;

int n,m,q,l[N],v[N],a[N],f[N];

int ck(int x,int y){
    double ans=0;
    for(int i=1;i<=n;i++){
        ans+=l[i]*1.0/(x+v[i]);
    }
//  cout<<x<<" "<<y<<": "<<ans<<endl;
    if(ans<=y) return 1;
    else return 0;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++) cin>>v[i];
    for(int i=1;i<=n;i++) cin>>l[i];
    cin>>m;
    for(int i=1;i<m;i++) cin>>a[i];
    for(int i=1;i<=m;i++) cin>>f[i];
    cin>>q;
    a[m]=inf;
    while(q--){
        int s,t; cin>>s>>t;
        int cha=t-s;
        int ll=0,r=m-1;
        while(ll<r){
            int mid=(ll+r)>>1;
            if(ck(a[mid],cha)) r=mid;
            else ll=mid+1;
        }
        if(!ck(a[ll],cha)) ll++;
        cout<<f[ll]<<endl;
    }
    return 0;
}