Codeforces Round #364 (Div. 2) D. As Fast As Possible 【二分+贪心+验证】

贪心加二分验证,将n分成每组最多k个人的小组来考虑,问题中要求的就是最后一组什么时候到达,然后可以想到的一点是,汽车最好在所有的时间里面都在运行,所以我们枚举第一组坐车多少米,之后他们就是步行到终点,计算一下这样的时间,并且假设这个是答案,在去验证剩下的小组能不能在这个时间内达到终点。

/* ***********************************************
Author        :Maltub
Email         :xiang578@foxmail.com
Blog          :htttp://www.xiang578.com
************************************************ */

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
//#include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define pb push_back
using namespace std;
typedef vector<int> VI;
typedef long long ll;
const ll mod=1000000007;
const int N=2048;
const double eps=1e-6;
int n,k;
double l,v1,v2;

int get(double t,double m)
{
    int p=n-1;
    double car=m,peo=m/v2*v1,nt=m/v2,T,L,x,tmp;
    while(p!=0)
    {
        tmp=(car-peo)/(v1+v2);
        nt+=tmp;
        peo+=tmp*v1;
        //printf("%lf %lf ",nt,t);
        if(nt>t) break;
        T=t-nt;
        L=l-peo;
        x=(L-v1*T)/(v2-v1);
        car=peo+x*v2;
        peo=peo+x*v1;
        nt+=x;
        if(nt>t) break;
        p--;
    }
    //printf("%d\n",p);
    if(p==0) return 1;
    else return 0;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);

    scanf("%d%lf%lf%lf%d",&n,&l,&v1,&v2,&k);
    if(n==k)
    {
        printf("%.10f\n",l/v2);
    }
    else
    {
        if(n%k==0) n=n/k;
        else n=n/k+1;
        double M,L=0,R=l,ans=1e9+10,t;
        while(fabs(R-L)>eps)
        {
            M=(L+R)/2;
            t=M/v2+(l-M)/v1;
            //printf("%lf %lf %lf ",L,R,t);
            if(get(t,M)==1)
            {
                ans=min(ans,t);
                L=M;
            }
            else
            {
                R=M;
            }
        }
        printf("%.10f\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值