题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6581
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
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
最后一辆车的车头到达stop线的时候可能会有以下n+1种情况:
也就是说在未给定这n+1辆车l,s,v的时候,每一辆车都有可能当成车头通过,对应通过的时间很好算。
对于给定的n+1辆车的l,s,v,把这些数据都分别带入上述n+1种情况,必定有一种是实际发生的,取最大值即为答案。
想了很久也很没用彻底想清楚为什么取最大值呢?
严格的证明我还没有想出,欢迎讨论。
!!!2个会超时的小点⚠️!!!:
(1)不要用cin 读,即使写了ios::sync_with_stdio(false); 也会超时,再写上
cin.tie(nullptr);
cout.tie(nullptr);
也还是会超时,所以放弃用cin读入吧,实在没办法的时候再用cin吧!!
(2)本题多组输入,但是它并没有给到底最大是多少组测试样例,如果每个测试样例都memset一下的话,时间复杂度O(ke5),k是测试次数,我就这样写了,然后T了。尽量避免这种情况吧,多组测试慎用memset!
#include
using namespace std;
const int maxn = 100010;
typedef long long ll;
double sum[maxn];// sum[i]记录了以第i辆车为车队的车头且最后一辆车到达stop线时要走的距离
double n, l, s, v, ans;
int main()
{
//freopen("/Users/zhangkanqi/Desktop/11.txt","r",stdin);
while(~scanf("%lf", &n))
{
sum[0] = 0;
for(int i = 0; i <= n; i++)
{
scanf("%lf", &l);
if(i) sum[i] = sum[i - 1] + l;
}
for(int i = 0; i <= n; i++)
{
scanf("%lf", &s);
sum[i] += s;
}
ans = 0;
for(int i = 0; i <= n; i++)
{
scanf("%lf", &v);
ans = max(ans, sum[i]/v);
}
printf("%.10lf\n", ans);
}
return 0;
}