On vacations n pupils decided to go on excursion and gather all together. They need to overcome the path with the length l meters. Each of the pupils will go with the speed equal to v1. To get to the excursion quickly, it was decided to rent a bus, which has seats for k people (it means that it can't fit more than k people at the same time) and the speed equal to v2. In order to avoid seasick, each of the pupils want to get into the bus no more than once.
Determine the minimum time required for all n pupils to reach the place of excursion. Consider that the embarkation and disembarkation of passengers, as well as the reversal of the bus, take place immediately and this time can be neglected.
The first line of the input contains five positive integers n, l, v1, v2 and k (1 ≤ n ≤ 10 000, 1 ≤ l ≤ 109, 1 ≤ v1 < v2 ≤ 109, 1 ≤ k ≤ n) — the number of pupils, the distance from meeting to the place of excursion, the speed of each pupil, the speed of bus and the number of seats in the bus.
Print the real number — the minimum time in which all pupils can reach the place of excursion. Your answer will be considered correct if its absolute or relative error won't exceed 10 - 6.
5 10 1 2 5
5.0000000000
3 6 1 2 1
4.7142857143
In the first sample we should immediately put all five pupils to the bus. The speed of the bus equals 2 and the distance is equal to 10, so the pupils will reach the place of excursion in time 10 / 2 = 5.
題意:現在有n個人,路程長度爲l,人行走速度爲v1,有一輛車,每次能載k人,車行走速度爲v2,每個人最多隻能坐一次車,求n個人走完l的最少花費時間。
思路:每個人只能坐一次車,所以最後到達終點的人決定總時間,可以看出,用車每次載k個人,行走一定的距離,然後折返載下一趟人追上上一趟的人這種情
況能得出最優值。所以假設第一趟車載人時間爲t1,所以兩邊的人相距v2*t1-v1*t1,假設第二趟車載人時間爲t2,因爲要追上上一趟,所以v2*t1-v1*t1+v1*t2=v2*t2,
所以t1=t2,也就是每人坐車的時間相等。
車載人的趟數:p=(n+k-1)/k。
設總時間爲t,每人坐車的時間t1,則v1*(t-t1)+v2*t1=l,所以t1可以用t表示,設折返時間爲t2,則(v2-v1)*t1=(v2+v1)*t2,而且t2*(p-1)+t1*p<=t。這樣二分時間t就
可以求出結果。
#include <bits/stdc++.h>
using namespace std;
double v1, v2, l;
int n, k;
bool check(double t){
double t2 = (l-t*v1)/(v2-v1);
double t3 = (v2-v1)*t2/(v2+v1);
int p = (n+k-1)/k;
return t2*p+(p-1)*t3<=t;
}
int main()
{
scanf("%d %lf %lf %lf %d", &n, &l, &v1, &v2, &k);
double ll = l/v2, r = l/v1, mid;
for(int i = 1;i <= 100000;i++){
mid = (ll+r)/2;
if(check(mid)) r = mid;
else ll = mid;
}
printf("%.7lf\n", r);
}