HDU 6581 [2019 Multi-University Training Contest 1]

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 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

思路

一开始想着因为只有最慢的一部车会堵住后面的车,所以干脆把所有车的通过时间都算一遍,求出最慢的就是答案。但是当时并没有考虑到可能有的车冲线后依然对后面的车造成影响,对于怎么解决它冲线后对后车的影响这个问题思索了很久。最后发现只要冲线一定距离就已经脱离影响了。所以当这辆车冲过终点线一定距离length后就可以不计算了,一定距离指的是它自身的车身长度和后面的车(除最后外)的车身长度总长。例如:倒数第二辆车的length是它自身的长度,倒数第三辆车的length是倒数第二辆和自身的长度。将一定距离加上距离终点线距离加在一起再除以它的速度即可求出时间,将所有车进行计算取时间最长为答案。
P.S.一直听说cin不好,第一次深受cin毒害,一开始用cin录入跑了5s还是TLE,全部改成scanf只跑了1.5s

标题

#include
using namespace std;
#define INF 0x3f3f3f3f
const int maxn = 2e6+5;
typedef long long ll;

struct node{
	int l,s,v;
}a[maxn];

int n;

int main() {
	while(scanf("%d",&n) != EOF) {
		for(int i=0;i<=n;i++) {
			scanf("%d",&a[i].l);
		}
		for(int i=0;i<=n;i++) {
			scanf("%d",&a[i].s);
		}
		for(int i=0;i<=n;i++) {
			scanf("%d",&a[i].v);
		}
		ll length = -a[0].l;
		double ans = 0;
		for(int i=0;i<=n;i++) {
			length += a[i].l;
			double temp = 1.0 * (a[i].s + length) / a[i].v;
			if(temp > ans) {
				ans = temp;
			}
		}
		printf("%.10lf\n",ans);
	}
}

题目来源

HDU 6581 Vacation

你可能感兴趣的:(HDU)