Vacation

题目描述

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.

思路

假定只有0号车这一辆的话,通过的时间就是路程除以速度。由于前面存在车辆,可能会造成阻塞,但是即便会造成阻塞,前车i号车到达固定位置的时间是不受后车影响的(假定i号车前面没有其他车辆的话)。计算除0号车以外其他车完全过线的时间,取最大值即可。

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

#include <cstdio>
#include <algorithm>

const size_t max_count = 100002;

int length[max_count], start[max_count], velocity[max_count];

void run(const int& count) {
for (int i = 0; i < count; i++) {
scanf("%d", &length[i]);
}
for (int i = 0; i < count; i++) {
scanf("%d", &start[i]);
}
for (int i = 0; i < count; i++) {
scanf("%d", &velocity[i]);
}

double res = (double)start[0] / velocity[0];
int distance = 0;
for (int i = 1; i < count; i++) {
distance += length[i];
double temp = (double)(start[i] + distance) / velocity[i];
res = std::max(res, temp);
}

printf("%.12lfn", res);
}

int main() {
int count;
while (~scanf("%d", &count)) {
run(count + 1);
}

return 0;
}

原文:大专栏  Vacation


猜你喜欢

转载自www.cnblogs.com/jimmykeji/p/11645284.html
今日推荐