Educational Codeforces Round 15 D 数学推导

来源:互联网 发布:留美幼童 知乎 编辑:程序博客网 时间:2024/06/05 08:04



链接:戳这里


D. Road to Post Office
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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个单位距离的地方,开车走一单位距离需要a秒。

但是每走k个单位的距离车会抛锚一次,然后需要花t秒修车。

人步行的速度是每一个单位的距离需要b秒

问最少多少时间能走完这d个单位的距离


思路:

人每开车k个距离,需要花费k*a+t秒。设他走了x个这样的k单位距离

那么有等式:(k*a+t)*x+(d-k*x)*b=Tmin,找出合适的x可以使得时间最少

化简等式为:(k*a-k*b+t)*x+d*b=Tmin

1:发现当(k*a-k*b+t) 为>=0的数的时候,就不适合开车了,直接开一段k单位距离然后开始步行。

2:当(k*a-k*b+t)为负数的时候,肯定是负的越多越好,所有直接开到最后。但是需要注意细节,当d个单位的距离正好是k的倍数的时候,直接开到终点然后到了终点不需要修车了。当d%k!=0的时候,开到最远的d/k*k距离。判断这里适合步行还是继续修车开车

3:k>=d的时候直接开车吧 (我听完你唱的歌,就上了车


代码:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<iomanip>#include<cmath>#include<bitset>#define mst(ss,b) memset((ss),(b),sizeof(ss))///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;typedef long double lb;#define INF (1ll<<60)-1#define Max 1e9using namespace std;const lb eps=1e-10;ll d,k,a,b,t;int main(){    cin>>d>>k>>a>>b>>t;    if(d<=k){        printf("%I64d\n",d*a);        return 0;    }    ll tmp=k*(a-b)+t;    if(tmp>=0){        printf("%I64d\n",k*a+(d-k)*b);        return 0;    }    ll cnt=d/k;    if(d%k==0){        printf("%I64d\n",tmp*cnt+d*b-t);        return 0;    }    ll dis=d-k*cnt;    ll t1=(k*a+t)*cnt+dis*a;    ll t2=(k*a+t)*cnt-t+dis*b;    printf("%I64d\n",min(t1,t2));    return 0;}


0 0