HDU - 6581 Vacation (三种方法) HDU多校第一场1004

题目描述了Tom和Jerry在一条一车道的路上驾驶,身后有n辆汽车,每辆车有长度、位置和最大速度。虽然绿灯常亮,但不能超车。目标是找出所有车辆通过停车线的最短时间。提供了三种解法:事件驱动法、二分查找法和O(n)直接计算法。

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

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=6581

题目:

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 nn cars in front of them. The iith car has a length of lili, the head of it is sisi from the stop-line, and its maximum velocity is vivi. The car Tom and Jerry are driving is l0l0 in length, and s0s0from the stop-line, with a maximum velocity of v0v0. 
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 00. 
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 nn (1≤n≤105,∑n≤2×1061≤n≤105,∑n≤2×106), the number of cars. 
The next three lines each contains n+1n+1 integers, li,si,vili,si,vi (1≤si,vi,li≤1091≤si,vi,li≤109). It's guaranteed that si≥si+1+li+1,∀i∈[0,n−1]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−610−6. 
Formally, let your answer be aa, and the jury's answer is bb. Your answer is considered correct if |a−b|max(1,|b|)≤10−6|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

题目大意:

n+1辆车,告诉你每辆车的距离终点的长度车身的长度最高行驶速度。在行驶的过程中不允许超车(即后面的车最多也只能车头贴着前一辆车的车尾,然后以同样的速度前进),问这些车均驶过终点需要多长时间(与答案误差不超过1e-6)。

题目分析:(参考官方题解)

第一种方法:

把第 i 辆车追上第 i + 1 辆车当作一个事件,显然只有 n 个事件,且第 i 辆车追上第 i+1 辆车只可能会对第 i−1 辆车追上第 i 辆车的时间产生影响且时间一定是变小,因此 可以维护车之间的距离和速度来计算事件发生时间,用堆来找出最早发生的事件,不停处理直到距离终点最远的车通过停车线。同时还需要记录行驶过程中,以相同速度前进的一系列车的最前面的车的编号,和最后面的车的编号,复杂度为 O(nlogn)。

具体实现细节见代码可知。

第二种方法:

可以直接二分最终时间,然后从第一辆车开始递推求出每辆车的最终位置。复杂度为 O(nlogC),也可以过。

二分给定一个mid,进行check的时候,我们只需要记录下来前一辆车车头最终能到达的位置-前一辆车的长度的值,然后计算当前这辆车的车头能到达的最远距离(在没有车挡路的情况下),然后在两者之间取一个min就是该车车头能到达的最终位置。递推计算最远的车能否通过终点即可。(任何一辆车达不到终点即可返回)

第三种方法:

O(n) 的做法,也是最简单的做法。最终通过停止线的时候,一定是一个车后面堵着剩余所有的车,那么影响时间的就只有最前面这辆车,所以对于每一辆车, 假设是它是和 0 车堵在一起的最靠前的一辆车,那么可以计算出一个值,所有的车的计算 值的最大值就是答案。计算的时候一定不要忘了算上这堵在一起的一堆车的车身长度,因为最后一辆车的车头通过终点,才算结束。

代码:

第一种方法:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>

using namespace std;

const int MAXN = 100000 + 100;
struct R {
    int id;
    double t;
    bool operator<(const R& temp) const { return t > temp.t; }
};
int l[MAXN], s[MAXN], v[MAXN], n;
int nex[MAXN], pre[MAXN];
double dis[MAXN], t[MAXN];
bool vis[MAXN];
priority_queue<R> q;

int find(int x) { return nex[x] == x ? x : nex[x] = find(nex[x]); }

int main() {
    while (scanf("%d", &n) != EOF) {
        for (int i = 0; i <= n; i++) scanf("%d", &l[i]);
        for (int i = 0; i <= n; i++) scanf("%d", &s[i]);
        for (int i = 0; i <= n; i++) scanf("%d", &v[i]);
        for (int i = 0; i <= n; i++)
            nex[i] = i, pre[i] = i, vis[i] = false, t[i] = 0;
        for (int i = 0; i < n; i++) {
            dis[i] = s[i] - s[i + 1] - l[i + 1];
            if (v[i] > v[i + 1]) q.push(R{i, dis[i] / (v[i] - v[i + 1])});
        }
        double cd = 0, cv = v[0], ct = 0;
        while (!q.empty()) {
            R now = q.top();
            q.pop();
            int id = now.id;
            if (vis[id]) continue;
            vis[id] = true;
            if (cd + (now.t - ct) * cv >= s[0]) {
                ct += (s[0] - cd) / cv;
                cd = s[0];
                break;
            }
            cd += cv * (now.t - ct);
            ct = now.t;
            nex[id] = nex[find(id + 1)];
            pre[find(id + 1)] = pre[id];
            if (pre[id] == 0) cv = v[nex[id]];
            dis[pre[id] - 1] -=
                ((v[pre[id] - 1] - v[id]) * (now.t - t[pre[id] - 1]));
            t[pre[id] - 1] = now.t;
            if (v[pre[id] - 1] > v[nex[id]])
                q.push(R{
                    pre[id] - 1,
                    dis[pre[id] - 1] / (v[pre[id] - 1] - v[nex[id]]) + now.t});
        }
        while (!q.empty()) q.pop();
        if (cd != s[0]) ct += (s[0] - cd) / cv;
        printf("%.10f\n", ct);
    }
    return 0;
}

第二种做法:

#include <algorithm>
#include <cmath>
#include <cstdio>

using namespace std;

const int MAXN = 100000 + 100;
const double EPS = 1e-7;
int l[MAXN], s[MAXN], v[MAXN], n;

bool check(double mid) {
    double cpos = mid * v[n] - s[n], temp;
    if (cpos < 0) return false;
    for (int i = n - 1; i >= 0; i--) {
        temp = mid * v[i] - s[i];
        cpos = min(temp, cpos - l[i + 1]);
        if (cpos < 0) return false;
    }
    return true;
}

int main() {
    while (scanf("%d", &n) != EOF) {
        long long sum = 0;
        for (int i = 0; i <= n; i++) scanf("%d", &l[i]);
        for (int i = 0; i <= n; i++) scanf("%d", &s[i]);
        for (int i = 0; i <= n; i++) scanf("%d", &v[i]);
        double ans = 0, r = 1e9 + 100, l = double(s[0]) / v[0], mid;
        while (fabs(r - l) > EPS) {
            mid = (l + r) / 2;
            if (check(mid)) {
                ans = mid;
                r = mid;
            } else {
                l = mid;
            }
        }
        printf("%.10f\n", ans);
    }
    return 0;
}

第三种做法:

#include <algorithm>
#include <cstdio>

using namespace std;

const int MAXN = 100000 + 100;
int l[MAXN], s[MAXN], v[MAXN], n;

int main() {
    while (scanf("%d", &n) != EOF) {
        long long sum = 0;
        for (int i = 0; i <= n; i++) scanf("%d", &l[i]);
        for (int i = 0; i <= n; i++) scanf("%d", &s[i]);
        for (int i = 0; i <= n; i++) scanf("%d", &v[i]);
        double ans = 1.0 * s[0] / v[0];
        for (int i = 1; i <= n; i++) {
            sum += l[i];
            ans = max(ans, (1.0 * (sum + s[i]) / v[i]));
        }
        printf("%.10f\n", ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值