HDU 6581 Vacation
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K(Java/Others)
Total Submission(s): 2173 Accepted Submission(s): 966
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]
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
Source
2019 Multi-University Training Contest 1
二分时间,最后误差在1e-7内的时候输出答案,具体代码如下
#include<iostream>
#include<cstdio>
using namespace std;
int n;
double s[100010],l[100010],v[100010],vmn,pos[100010];//pos存的是车子行进一段时间后与终点线的距离,可正可负
int main()
{
while(~scanf("%d",&n))
{
vmn=1e10;
for(int i=0;i<=n;i++)
scanf("%lf",l+i);//车长
for(int i=0;i<=n;i++)
scanf("%lf",s+i);//车头距离终点线的长度
for(int i=0;i<=n;i++)
scanf("%lf",v+i),vmn=v[i]<vmn?v[i]:vmn;//车子的速度,并用vmn存最小车速
if(!n)//如果n=0,即只有一辆车,则直接路程除以时间得出结果
{
printf("%.10lf\n",s[0]/v[0]);
continue;
}
double left=0,right=s[0]/vmn;
//利用二分来查找时间
while(right-left>=1e-7)
{
double mid=(right+left)/2;
for(int i=n;i>=0;i--)
pos[i]=s[i]-v[i]*mid;
//计算车运行mid时间后相对终点线的距离,可正可负
for(int i=n-1;i>=0;i--)
if(pos[i]<pos[i+1]+l[i+1])//如果后面的车越过前面的车,则把这辆车重新置于后面
pos[i]=pos[i+1]+l[i+1];
if(pos[0]<0)
right=mid;
else left=mid;
//时间多了左缩区间,时间少了右缩区间
}
printf("%.10lf\n",left);
}
return 0;
}