hdu 6581 Vacation思维
Posted satan666
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 6581 Vacation思维相关的知识,希望对你有一定的参考价值。
Vacation
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Special Judge
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辆车按前后顺序在一条单行道上行驶,若后一辆车碰到了前一辆车的尾巴,也必须按前一辆车的速度行驶,不能超越,按离终点距离从大到小的顺序给你n+1辆车的车身长,车头离终点距离,和各自的速度(你是离终点最远的那一辆车),问你冲过终点的最短时间。
思路:这道题我们不应该把重点放在每一辆车何时冲过终点,不然这题会变得非常复杂,而是应该考虑冲过终点后它何时能留出足够的空间让后面的车放下,这才是我们真正应该求的临界状态,比如第一辆车长度是3,后面一辆长度是2,则第一辆车至少要车头冲过终点线并且再走5的长度才能让后面一辆车放下(它自己的车身3+后面那辆车的长度2),说的明白点,就是至少要再跑除了我们自己那辆车之外的所有车的长度(因为我们自己的车只要冲过终点就行了,所以前面的车不需要给我们的车留空间),这样就能让后面的车全部都跑出来,这样我们的车才能冲过终点。要是冲过那个临界位置我们就不用管了呀,所以我们只要考虑临界位置就好了。那么问题又来了,要是后面的车被前面的车堵住了怎么办?我们可以先思考一下,什么情况下后面一辆车会被前面一辆车给堵住?当然是后面一辆车到达它自己应该到的那个位置时,前面一辆车却没有到达前面一辆车它自己应该到达的位置(也就是各自的临界位置),这样他们肯定在行驶过程中相碰了。所以真正用的时间就是最慢到达自己临界位置的那辆车所用的时间(因为后面的车全部被这辆“拖拉机”堵住了),如此类推下去,我们需要的时间就是每一辆车达到它应该到达的位置的时间的最大值,此题结束。
代码:
1 #include "stdio.h"
2 #include "algorithm"
3 using namespace std;
4 const int N=1e5+7;
5 int n,l[N],s[N],v[N],sum[N];
6 double t,t1;
7
8 int main()
9 while(~scanf("%d", &n))
10
11 t=-1; ///初始化一下答案
12 for (int i = 1; i <= n + 1; i++)
13 scanf("%d", &l[i]);
14 for (int i = 1; i <= n + 1; i++)
15 scanf("%d", &s[i]);
16 for (int i = 1; i <= n + 1; i++)
17 scanf("%d", &v[i]);
18 for(int i=2;i<=n+1;i++)
19 sum[i]=sum[i-1]+l[i]; ///先把车身长度也就是每辆车需要预留的位置给预处理出来
20 for(int i=n+1;i>=1;i--)
21
22 t1=(s[i]+sum[i])*1.0/v[i]; ///这辆车到达它应该到达的位置的位置所用的时间
23 t=max(t,t1); ///找出那辆最慢的“拖拉机”,当i=1时,t代表我们没有被挡直接冲过终点所用的时间
24
25 printf("%.10f\n",t);
26
27 return 0;
28
谢谢访问,如果觉得好的话可以点个赞哦,有不懂的也可以在下面提问,互相学习,共同进步,谢谢大家的支持!
以上是关于hdu 6581 Vacation思维的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #645 (Div. 2) D - The Best Vacation 二分+思维