codeforces 701D D. As Fast As Possible(数学)

题目链接:

D. As Fast As Possible

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 kpeople (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.

Input

The first line of the input contains five positive integers nlv1, 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.

Output

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.

Examples
input
5 10 1 2 5
output
5.0000000000
input
3 6 1 2 1
output
4.7142857143


题意:

现在给了两个速度,n个人,长为l的路,车的速度快,人的速度,车的容量为k;
现在问这n个人走完l的路后用时最少是多少;

思路:

我是二分车第一次载人的时长,计算最后一次载人是否恰好到达终点;
精度被卡我就把二分的次数上限限定了才过,好像直接有公式;

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>

using namespace std;

#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));

typedef  long long LL;

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('
');
}

const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e5+10;
const int maxn=500+10;
const double eps=1e-14;

int n,k;
double L,v1,v2;

int check(double x)
{
        double t=x,pos=v2*t;
        For(i,2,n)
        {
            double tempt=t,temppos=pos;
            pos=(temppos-tempt*v1)*v2/(v2-v1)+(temppos-tempt*v1)*v1/(v1+v2)+tempt*v1;
            t=(temppos-tempt*v1)/(v2-v1)+(temppos-tempt*v1)/(v1+v2)+tempt;
        }
        if(pos>=L)return 0;
        return 1;
}

int main()
{
        scanf("%d%lf%lf%lf%d",&n,&L,&v1,&v2,&k);
        if(k>=n)printf("%.10lf",L/v2);
        else
        {
            if(n%k==0)n=n/k;
            else n=n/k+1;
            double l=0,r=L/v2;
            int cnt=0;
            while(r>=l+eps&&cnt<1000)
            {
            	cnt++;
                double mid=(l+r)/2;
                if(check(mid))l=mid;
                else r=mid;
            }
			r=(l+r)/2;
            double ans=(L-r*v2)/v1+r;
            printf("%.10lf
",ans);
        }
        return 0;
}

  

原文地址:https://www.cnblogs.com/zhangchengc919/p/5698410.html