杭电多校第一场1004[优先队列+模拟]

本文解析了一道关于车辆追击问题的算法竞赛题目,通过优先队列和并查集的方法,解决多辆车在受前车速度限制的情况下,计算最后一辆车通过终点线的最短时间问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1004 Vacation
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2394 Accepted Submission(s): 1079
Special Judge

Problem Description
Tom and Jerry are going on a vacation. They are now driving on a one-way road and several cars are in front of them. To be more specific, there are n cars in front of them. The ith car has a length of li, the head of it is si from the stop-line, and its maximum velocity is vi. The car Tom and Jerry are driving is l0 in length, and s0 from the stop-line, with a maximum velocity of v0.
The traffic light has a very long cycle. You can assume that it is always green light. However, since the road is too narrow, no car can get ahead of other cars. Even if your speed can be greater than the car in front of you, you still can only drive at the same speed as the anterior car. But when not affected by the car ahead, the driver will drive at the maximum speed. You can assume that every driver here is very good at driving, so that the distance of adjacent cars can be kept to be 0.
Though Tom and Jerry know that they can pass the stop-line during green light, they still want to know the minimum time they need to pass the stop-line. We say a car passes the stop-line once the head of the car passes it.
Please notice that even after a car passes the stop-line, it still runs on the road, and cannot be overtaken.

Input
This problem contains multiple test cases.
For each test case, the first line contains an integer n (1≤n≤105,∑n≤2×106), the number of cars.
The next three lines each contains n+1 integers, li,si,vi (1≤si,vi,li≤109). It’s guaranteed that si≥si+1+li+1,∀i∈[0,n−1]

Output
For each test case, output one line containing the answer. Your answer will be accepted if its absolute or relative error does not exceed 10−6.
Formally, let your answer be a, and the jury’s answer is b. Your answer is considered correct if |a−b|max(1,|b|)≤10−6.
The answer is guaranteed to exist.

Sample Input
1
2 2
7 1
2 1
2
1 2 2
10 7 1
6 2 1

Sample Output
3.5000000000
5.0000000000

Source
2019 Multi-University Training Contest 1

题意:按照距离终点线的长短顺序给出n+1辆车距离终点线的距离Si,最高速度Vi,车长Li,问最远的那辆车什么时候可以到终点线

题解:由于每辆车被前面的车堵住之后只会影响它后面的车追上它的时间并且这个时间是减小的,所以可以把这n个追击问题丢到优先队列里,借助优先队列每次取出最先发生的时间,这个过程可以得到每个追击问题完成的时间,接下来模拟即可。(追击过程可以借助并查集来维护追击之后连在一起的车从而维护每个车真正会影响到的"后面的车"(也就是同一个集合里最左边的车后面的车))

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug(x) cout<<#x<<" is "<<x<<endl;
const int maxn=1e5+5;
struct pot{
    int id;
    double tim;
    double dis;
    bool operator<(const struct pot &aa)const{
        return tim>aa.tim;
    }
};
struct pot2{
    int id;
    double tim;
}acc[maxn];
bool cmp(struct pot2 aa,struct pot2 bb){
    return aa.tim<bb.tim;
}
double vis[maxn];
ll l[maxn],s[maxn],v[maxn],fa[maxn],L[maxn],fa2[maxn],sum[maxn];
int finds(int x){
    int xx=x;
    while(fa[x]!=x){
        x=fa[x];
    }
    while(fa[xx]!=x){
        int t=fa[xx];
        fa[xx]=x;
        xx=t;
    }
    return x;
}
int find2(int x){
    int xx=x;
    while(fa2[x]!=x){
        x=fa2[x];
    }
    while(fa2[xx]!=x){
        int t=fa2[xx];
        fa2[xx]=x;
        xx=t;
    }
    return x;
}
int main(){
    int n;
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    while(scanf("%d",&n)!=EOF){
        for(int i=1;i<=n+1;i++)vis[i]=-1;
        for(int i=1;i<=n+1;i++)L[i]=fa[i]=i;
        for(int i=1;i<=n+1;i++){
            scanf("%lld",&l[i]);
            sum[i]=sum[i-1]+l[i];
        }
        for(int i=1;i<=n+1;i++){
            scanf("%lld",&s[i]);
        }
        for(int i=1;i<=n+1;i++){
            scanf("%lld",&v[i]);
        }
        priority_queue<struct pot>pq;
        while(!pq.empty())pq.pop();
        for(int i=1;i<=n;i++){
            struct pot ac;
            if(v[i]>=v[i+1]&&(s[i]-s[i+1]-l[i+1])==0){
                ac.id=i;
                ac.tim=0;
                ac.dis=ac.tim*v[ac.id];
                pq.push(ac);
                continue;
            }
            if(v[i]<=v[i+1])continue;
            ac.id=i;
            ac.tim=1.0*(s[i]-s[i+1]-l[i+1])/(v[i]-v[i+1]);
            ac.dis=ac.tim*v[ac.id];
            pq.push(ac);
        }
        while(!pq.empty()){
            struct pot ak=pq.top();
            pq.pop();
            if(vis[ak.id]!=-1)continue;
            vis[ak.id]=ak.tim;
            int f1=finds(ak.id+1);
            int f2=finds(ak.id);
            if(L[f2]>1&&vis[L[f2]-1]==-1&&v[L[f2]-1]>v[f1]){
                struct pot cxk;
                cxk.id=L[f2]-1;
                cxk.tim=ak.tim+1.0*(s[L[f2]-1]-(s[f2]-ak.dis+sum[f2]-sum[L[f2]-1])-ak.tim*v[L[f2]-1])/(v[L[f2]-1]-v[f1]);
                cxk.dis=cxk.tim*v[L[f2]-1];
                pq.push(cxk);
            }
            fa[f2]=f1;
            L[f1]=min(L[f1],L[f2]);
        }
        double ans=0;
        double ww=1.0*s[1]/v[1];
        if(vis[1]==-1||ww<=vis[1]){
            ans=ww;
        }
        else{
            int tot=0;
            for(int i=1;i<=n;i++){
                if(vis[i]==-1)continue;
                acc[++tot].id=i;
                acc[tot].tim=vis[i];
            }
            sort(acc+1,acc+tot+1,cmp);
            for(int i=1;i<=n+1;i++)fa2[i]=i;
            double res=s[1];
            for(int i=1;i<=tot;i++){
                int f1=find2(acc[i].id+1);
                int f2=find2(acc[i].id);
                int f3=find2(1);
                if(res>(acc[i].tim-ans)*v[f3]){
                    res-=(acc[i].tim-ans)*v[f3];
                    ans=acc[i].tim;
                }
                else{
                    ans+=res/v[f3];
                    res=0;
                    break;
                }
                fa2[f2]=f1;
            }
            if(res){
                int ff=find2(1);
                ans+=res/v[ff];
                res=0;
            }
        }
        printf("%.10f\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值