Codeforces 702D Road to Post Office【捎带数学的思维题】

D. Road to Post Office
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasiliy has a car and he wants to get from home to the post office. The distance which he needs to pass equals to d kilometers.

Vasiliy's car is not new — it breaks after driven every k kilometers and Vasiliy needs t seconds to repair it. After repairing his car Vasiliy can drive again (but after k kilometers it will break again, and so on). In the beginning of the trip the car is just from repair station.

To drive one kilometer on car Vasiliy spends a seconds, to walk one kilometer on foot he needs b seconds (a < b).

Your task is to find minimal time after which Vasiliy will be able to reach the post office. Consider that in every moment of time Vasiliy can left his car and start to go on foot.

Input

The first line contains 5 positive integers d, k, a, b, t (1 ≤ d ≤ 1012; 1 ≤ k, a, b, t ≤ 106; a < b), where:

  • d — the distance from home to the post office;
  • k — the distance, which car is able to drive before breaking;
  • a — the time, which Vasiliy spends to drive 1 kilometer on his car;
  • b — the time, which Vasiliy spends to walk 1 kilometer on foot;
  • t — the time, which Vasiliy spends to repair his car.
Output

Print the minimal time after which Vasiliy will be able to reach the post office.

Examples
Input
5 2 1 4 10
Output
14
Input
5 2 1 4 5
Output
13
Note

In the first example Vasiliy needs to drive the first 2 kilometers on the car (in 2 seconds) and then to walk on foot 3 kilometers (in 12 seconds). So the answer equals to 14 seconds.

In the second example Vasiliy needs to drive the first 2 kilometers on the car (in 2 seconds), then repair his car (in 5 seconds) and drive 2 kilometers more on the car (in 2 seconds). After that he needs to walk on foot 1 kilometer (in 4 seconds). So the answer equals to 13 seconds.


题目大意:

给你一个长度为D的路程,有两种行进方式,一种是选择用车行进,但是车子每行进K长度的路程之后,就会坏掉,如果想继续使用,就要用t时间去修复。

车子行进1单位要用a时间,步行1单位要用b时间,随时都可以弃车开始选择步行,问最短时间到达目的地。


思路:


1、首先将问题分成两种情况:

①d<k

②d>=k

对于第①种情况,也就是不需要修车子就可以选择车子驾驶到终点的话,那么output=min(d*a,d*b);题目中保证了a<b.,那么答案一定是d*a;


2、然后考虑第②种情况,我们首先肯定一点,如果我们修了一次车的话,车子肯定要行进k单位长度是最优的,因为修完一次车子之后 ,在k单位内,车子行进一定是快于步行的。

那么问题关键就锁定在了修车子上来。

那么对于这种大情况,我们还可以分出来三种小情况,逐个分析:

①从起点到位子K用车,剩下部分就步行了。

②从起点到位子d/k*k用车,剩下部分用步行。

③从起点到终点都用车。

对于三种情况分别计数取最小即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define ll __int64

int main()
{
    ll d,k,a,b,t;
    while(~scanf("%I64d%I64d%I64d%I64d%I64d",&d,&k,&a,&b,&t))
    {
        if(d<k)
        {
            if(d*a<=d*b)printf("%I64d\n",d*a);
            else printf("%I64d\n",d*b);
        }
        else
        {
            ll output;
            output=d*b;
            // one;
            output=min(output,k*a+(d-k)*b);
            // two;
            ll contz=d/k;
            ll yu=d%k;
            ll tmp=(contz-1)*t+contz*k*a;
            tmp+=yu*b;
            output=min(output,tmp);
            //three
            ll tmp2=(contz)*t+contz*k*a;
            tmp2+=yu*a;
            output=min(output,tmp2);
            printf("%I64d\n",output);
        }
    }
}













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值