Codeforces 702D - Road to Post Office
来源:互联网 发布:俯卧撑多少组 知乎 编辑:程序博客网 时间:2024/05/22 18:19
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.
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.
Print the minimal time after which Vasiliy will be able to reach the post office.
5 2 1 4 10
14
5 2 1 4 5
13
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;}
- codeforces-702D-Road to Post Office
- Codeforces 702D - Road to Post Office
- Codeforces Problem 702D Road to Post Office(分类讨论)
- Codeforces 702D Road to Post Office(模拟 + 公式推导)
- CF 702D Road to Post Office
- Educational Codeforces Round 15 D. Road to Post Office
- CodeForces-702D Road to Post Office(初中数学脑洞题)
- Codeforces 702D Road to Post Office【捎带数学的思维题】
- Educational Codeforces Round 15 D. Road to Post Office (数学)
- Educational Codeforces Round 15 D Road to Post Office(分类讨论)
- CodeForces702D. Road to Post Office(二分or数学)
- CodeForces 34D Road Map
- Codeforces Gym100425 D. Toll Road
- CodeForces 828D High Road
- coderforces 702DRoad to Post Office
- CodeForces 831D Office Keys
- Codeforces 831 D Office Keys
- codeforces 831D Office Keys
- 开博客,走这条路的初衷与自勉记录
- frameset和iframe框架讲解
- jQuery 从零开始学习 (三) 属性与css样式
- tjut 4686
- 单例模式的设计
- Codeforces 702D - Road to Post Office
- 暑期集训训练3练习题B - Toxophily(HDU2298)
- PhantomJS快速入门教程(服务器端的 JavaScript API 的 WebKit)
- HashMap HashTable ConcurrentHashmap
- Java反射获取泛型
- Stage5--Python GUI编程TKinter
- 粉扑怎么用如何用粉扑上粉底
- CentOS系统如何安装Nginx+PHP+MySQL
- Swift:Foundation框架中的NS前缀的由来