HDU6581:Vacation
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
l
i
l_i
li, the head of it is
s
i
s_i
si from the stop-line, and its maximum velocity is
v
i
v_i
vi. The car Tom and Jerry are driving is
l
0
l_0
l0 in length, and s0 from the stop-line, with a maximum velocity of
v
0
.
v_0.
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≤
1
0
5
10^5
105,∑n≤2×
1
0
6
10^6
106), the number of cars.
The next three lines each contains n+1 integers, li,si,vi (1≤
s
i
,
v
i
,
l
i
≤
1
0
9
s_i,v_i,l_i≤10^9
si,vi,li≤109). It’s guaranteed that
s
i
≥
s
i
+
1
+
l
i
+
1
s_i≥s_{i+1}+l_{i+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−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
题目大意:
有一列车在等红绿灯,给出每辆车车长,自身到达线的距离和和车的速度。问你最后一辆车到达线时的时间。
(由于前车的速度有可能会比后面车的漫,导致前面的车挡住后面的车。)
题目思路:
我们可以先确定每辆车一定要到达的距离。
因为要使最后的车到达线,所以前面的车要给后面的车空出一定的位置,后面的车才能依次通过。
所以每辆车都要走的总路程=自身到达线的距离+后面所有车的车长(最后一辆车除外)
(因为最后一辆车是不用通过的,只要到达即可)
所以我们可以算出每辆车到达必达点的时间(总路程/速度)。
若前面到达必达点的时间比后面的车长,证明后面的车会被挡住,导致后面到达的时间和前面的相同。
所以我们只需要求出到达必达点最长时间即可。复杂度O(n)
hdu会卡cin(TLE),大家提交记得用scanf鸭( ̄▽ ̄)"
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=1e5+5;
const ll Mod=998244353;
ll l[maxn],s[maxn],v[maxn],sum[maxn];
int main(){
int n;
while(~scanf("%d",&n)){
for(int i=0;i<=n;i++){
scanf("%lld",&l[i]);
}
for(int i=0;i<=n;i++){
scanf("%lld",&s[i]);
}
for(int i=0;i<=n;i++){
scanf("%lld",&v[i]);
}
for(int i=0;i<=n;i++){
if(i) sum[i]=sum[i-1]-s[i-1]+s[i]+l[i];
else sum[i]=s[i];
}
double ans=0;
for(int i=0;i<=n;i++){
ans=max(ans,sum[i]*1.0/v[i]);
}
printf("%.10f\n",ans);
}
return 0;
}