Vacation(HDU-6581)

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

题意:给出 n+1 辆车的车长 l[i]、车头到终点的距离 s[i]、速度 v[i],问离第一辆车到终点的时间是多少

思路:

在车前进的过程中,如果位于前面的第 i 辆车的速度比位于其后面的第 i-1 辆车的速度小,那么由于后面的车必然会追上前面的车,因此可以将后面第 i-1 辆车与前面的第 i 辆车看作一辆车

因此,对于除去第一辆车外的每一辆车 i,其车尾越过终点的距离为:dis[i]=s[i]+(l[i]+l[i-1]+l[i-2]+...+l[1]),其花费的时间为 dis[i]/v[i]

故而求车长 l[i] 的前缀和,然后计算 dis[i],求取所有车的最大值即可

Source Program

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL quickModPow(LL a,LL b,LL mod){ LL res=1; a=a%mod; while(b){if(b&1)res=(a*res)%mod; a=(a*a)%mod; b>>=1;} return res; }
LL getInv(LL a,LL mod){ return quickModPow(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-10;
const int MOD = 998244353;
const int N = 1000000+5;
const int dx[] = {-1,1,0,0,1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
   
int l[N],s[N],v[N];
int sum[N];
int main() {
    int n;
    while(scanf("%d",&n)!=EOF) {
        sum[0]=0;
        for(int i=0;i<=n;i++) {
            scanf("%d",&l[i]);
            if(i!=0)
                sum[i]=sum[i-1]+l[i];
        }

        for(int i=0;i<=n;i++) {
            scanf("%d",&s[i]);
            sum[i]+=s[i];
        }
        for(int i=0;i<=n;i++)
            scanf("%d",&v[i]);

        double res=0;
        for(int i=1;i<=n;i++)
            res=max(res,(double)sum[i]/v[i]);
        
        res=max(res,(double)sum[0]/v[0]);
        printf("%.10f\n",res);
    }
    return 0;
}

 

你可能感兴趣的:(#,HDU,#,基础算法——模拟&思维)