Codeforces 702D - Road to Post Office

来源:互联网 发布:俯卧撑多少组 知乎 编辑:程序博客网 时间:2024/05/22 18:19
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 ≤ 10121 ≤ k, a, b, t ≤ 106a < 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.

有两种想法,先说最开始想到的吧:假设要走x段汽车那么所需要的时间为x*(a*k+t),剩下的部分走路,那么总时间为T=x*(a*k+t)-t+(d-x*k)*b为何其中要减去t呢?因为在最后一段用汽车时是不用修车的,所以要把修车的时间减去。这样就初步得到了一个公式。将这个公式整理变形可以得到

T=x*(a*k+t-b*k)+b*d-t,发现T的增减性与x前的系数有关,所以要分别讨论:

1.当a*k+t-b*k>=0时,此时x在正半轴是单增的,所以要保证T最小,要使x尽可能小,不妨令x=1,因为第一段路开车一定是比走路快的(这里ak+t和b*k可以理解为平均速度),所以T=a*k-b*k+b*d;

2.当a*k+t-b*k<0时,x在正半轴是单减的,要保证T最小,要使x尽可能大,所以总路程d中一共包含d/k段可以开车的路,所以不妨令x=d/k;剩下的d-(d/k)*k段有两种选择1)步行,2)开车

若是步行,则还是原公式T1=(d/k)*(a*k+t-b*k)-t+b*d 若是开车则需要等待t秒则T2=(d/k)*(a*k+t)+(d-(d/k)*k)*a,综上当系数<0时T=min(T1,T2);

之后简单来分析下当k>d时,T=d*a就好了。

第二种想法是由公式推导后想出,观察T=x*(a*k+t-b*k)+b*d-t,可以发现b*d是全程步行,a*k+t-b*k为时间差,也就是说,可以考虑先全程步行,再用开车来代替某段步行,这样可以直接推出公式。

#include<stdio.h>#include<iostream>#include<string>#include<string.h>#include<algorithm>using namespace std;#define ll long longint main(){    ll d,k,a,b,t,ans;    cin>>d>>k>>a>>b>>t; ll x=d/k;if (k>d) {cout<<d*a<<endl;return 0;}    if (k*a+t-k*b>0) ans=k*a-k*b+d*b;    else ans=min(x*(k*a-k*b+t)+d*b-t,x*(k*a+t)+(d-x*k)*a);    cout<<ans<<endl;}


0 0
原创粉丝点击