杭电多校2019 HDU 6581 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.

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辆车,车头到终点线的距离,自身车长,和该车的最大速度;由于道宽狭窄,只容一车通过,当后方车辆将要超过前方车辆,后方车辆速度将与前方车辆速度保持一致,问你让最后一辆车通过终点线需要的时间.

题目思路

如果需要让最后一辆车通过终点线,很容易想到,前方车辆必须给后方车辆腾出位置,那么此时一定也通过了终点线,那么前方车辆通过的距离有两部分,一是初始时刻距离终点线的距离,二是该车后方车辆的车长;
每辆车都要走的总路程=自身到达线的距离+后面所有车的车长(最后一辆车除外)
通过下图来理解
杭电多校2019 HDU 6581 Vacation

可以看出
1号车 走的距离 为sum[1]=s[1]
2号车 走的距离 为sum[2]=sum[1]+s[2]-s[1]+L[2];
3号车 走的距离 为sum[3]=sum[2]+s[3]-s[2]+L[3];
3号车 走的距离 为sum[4]=sum[3]+s[4]-s[3]+L[4];

代码

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <map>
#include <vector>
#define endl '\n'
using namespace std;
void IOS(){
 ios::sync_with_stdio(false);
 cin.tie(0);
 cout.tie(0);
}
typedef long long ll;
const int maxn=1e5+5;
int L[maxn],s[maxn],v[maxn],sum[maxn];
int main(){
 int n;
 while(~scanf("%d",&n))
 {
 	for(int i=1;i<=n+1;i++)
 	{
    scanf("%d",&L[i]);		
  }
  for(int i=1;i<=n+1;i++)
 	{
    scanf("%d",&s[i]);		
  }
  for(int i=1;i<=n+1;i++)
 	{
    scanf("%d",&v[i]);		
  }
  //分析一下,让求最后一辆车经过的时间
  //由于道路是狭窄的,只容一个车的宽度,所以后面的车要想穿过 最后的终点,前面的车一定要通过终点,且前面车还要多走一段,后面车的车身的距离
  //可以列出距离式子 非最后一辆车S=自身到终点的距离+后面车的车身长(给后面车腾位置)  最后一辆车=到终点的距离 
  //                                      
  //思路就出现了
  //最大时间一定是所求的 
  double ans=0;
  double temp=0;
  double LL=0;
  for(int i=1;i<=n+1;i++)
  {   if(i==1)
      {   sum[i]=s[i];
         
  	}
  	else
  	{
  	   	sum[i]=sum[i-1]+s[i]-s[i-1]+L[i];
  	   	
  	}
     
   } 
   for(int i=1;i<=n+1;i++)
   {
   	ans=max(ans,sum[i]*1.0/v[i]);
   }
   printf("%.10lf\n",ans);
 }
 return 0;
}


上一篇:【粤开医药行业深度】CAR~T细胞疗法:行远自迩,踔厉奋发


下一篇:MVC+EF+esayui初试(一 布局加菜单显示)