HDU6581 Vacation (HDU2019多校第一场1004)

HDU6581 Vacation (HDU2019多校第一场1004)

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

题意:

给你n+1辆汽车,每辆汽车有汽车的长度L,汽车距离终点的距离S,汽车的最大速度V

汽车与汽车之间距离为0时,速度大的汽车的速度会等于速度小的汽车的速度

求离终点最远的那辆车的车头到达终点的时间

注意,当汽车驶过终点线后会继续行驶,相遇,对后面的车也会造成影响

题解:

这里谈一下O(n)的写法

首先,当我们的最后一辆车不会与其他车相遇时,我们能得到答案为s[0]/v[0];

然后,我们假设,所有车都相遇了的情况

如图所示,最后一辆车刚好到达终点时,与到数第二辆车相遇,于此同时,这n+1辆车现在全部以第一辆车的速度v[n]行驶,那么我们也很容易求得,最后一辆车的车头到达终点的时间恰好是第一辆车到达pos位置的时间

\(t=(\sum_{1}^{n}L_i+sn)/v_n\)

有了这两种极限情况我们就可以想,如果我们的最后一辆车与前面任意一辆车相撞,我们最后一辆车最后到达终点的时间都是用相撞的最前面那辆车来计算的,每多撞上一辆车,我们的时间都响应的会变慢,也就是变大,于是我们保存的最大值就是最后一辆车相遇的时间了,如图所示,我就算到倒数第三辆车就算出答案了

代码:

#include <set>
#include <map>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
LL quick_pow(LL x, LL y) {
    LL ans = 1;
    while(y) {
        if(y & 1) {
            ans = ans * x % mod;
        } x = x * x % mod;
        y >>= 1;
    } return ans;
}
int l[maxn], s[maxn], v[maxn];
int main() {
#ifndef ONLINE_JUDGE
    FIN
#endif
    int n;
    while(~scanf("%d", &n)) {
        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 = s[0] * 1.0 / v[0];
        int sum = 0;
        for(int i = 1; i <= n; i++) {
            sum += l[i];
            ans = max(ans, (sum + s[i]) * 1.0 / v[i]);
        }
        printf("%.10f\n", ans);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/buerdepepeqi/p/11229427.html
今日推荐