Codeforces 702D Road to Post Office(模拟 + 公式推导)
2016-08-01 15:24
489 查看
题目链接:http://codeforces.com/problemset/problem/702/D
[b]题意:[/b]
一个人要去邮局取东西,从家到达邮局的距离为 d, 它可以选择步行或者开车,车每走 k 公里就要花费 t秒修一次才可以继续开,车每公里花费 a秒,步行每公里花费 b秒。依次给出d, k, a, b, t。问最少需要花费多少时间到达邮局。车刚开始时是好的。
[b]思路:[/b]
首先可以模拟一下,可以想到,如果车速比步速块,那么前 k公里路肯定选择坐车,因为开始时车是好的,不用花费多余的时间来修理车,否则直接全程步行就好了(步速大于车速,必选步行)。接下来我们把剩余的 d - k (d > k) 公里路分为两段,设 othr = d % k, car = d / k,则就分为 car * k 和 othr两段路。对于前car * k 公里路,如果选择开车(此时车已经坏了),那么总时间就等于开车所需要的时间加上修车所需要的时间, t1 = car * k * a + car * t, 如果选择步行,那么t2 = car * k * b;选择时间短的就好了。对于剩余的 othr 公里路开车需要 t + othr * a, 步行需要 othr * b,同样选择比较小的就好了。
注意:
有可能d < k,那么此时直接比较车速和步速,哪个快选择哪个就好了。
[b]代码:[/b]
[b]题意:[/b]
一个人要去邮局取东西,从家到达邮局的距离为 d, 它可以选择步行或者开车,车每走 k 公里就要花费 t秒修一次才可以继续开,车每公里花费 a秒,步行每公里花费 b秒。依次给出d, k, a, b, t。问最少需要花费多少时间到达邮局。车刚开始时是好的。
[b]思路:[/b]
首先可以模拟一下,可以想到,如果车速比步速块,那么前 k公里路肯定选择坐车,因为开始时车是好的,不用花费多余的时间来修理车,否则直接全程步行就好了(步速大于车速,必选步行)。接下来我们把剩余的 d - k (d > k) 公里路分为两段,设 othr = d % k, car = d / k,则就分为 car * k 和 othr两段路。对于前car * k 公里路,如果选择开车(此时车已经坏了),那么总时间就等于开车所需要的时间加上修车所需要的时间, t1 = car * k * a + car * t, 如果选择步行,那么t2 = car * k * b;选择时间短的就好了。对于剩余的 othr 公里路开车需要 t + othr * a, 步行需要 othr * b,同样选择比较小的就好了。
注意:
有可能d < k,那么此时直接比较车速和步速,哪个快选择哪个就好了。
[b]代码:[/b]
#include <iostream> #include <cmath> #include <cstdio> #include <cstring> #include <cstdlib> #include <stack> #include <queue> #include <vector> #include <algorithm> #include <string> #define mearv(a, b) memset(a, b, sizeof(a)) #define mestrc(a, b, c) memset(&(a), b, sizeof(c)) typedef long long LL; using namespace std; const int MAXN = 100000; const LL INF = 1e15; int main() { LL d, k, a, b, t; scanf("%I64d%I64d%I64d%I64d%I64d", &d, &k, &a, &b, &t); LL ans = 0; if(a < b) { if(d > k) ans += k * a, d -= k;//此时可以把路程分为两段。 else ans += d * a, d = 0;//总路程小于乘一次车的路程,则直接开车走完全程 } else ans += d * b, d = 0;//b步速大于车速,步行走完全程 if(d > 0) { LL othr = d % k, car = d / k; if(car * (t + k * a) < car * k * b) ans += car * (t + k * a); //前car * k 公里的选择 else ans += car * k * b; if(othr * b < t + othr * a) ans += othr * b; //后 othr公里的选择 else ans += t + othr * a; } printf("%I64d\n", ans); return 0; }
相关文章推荐
- Codeforces 702D Road to Post Office(模拟 + 公式推导)
- CodeForces 340C Tourist Problem(组合排列,公式推导)
- Codeforces 534C Polycarpus' Dice(数学,公式推导)
- CodeForces 165C Another Problem on Strings(公式推导)
- Codeforces 599D Spongebob and Squares 【公式推导 暴力】
- CodeForces 185A Plant(公式推导+快速幂)
- Codeforces 758C Unfair Poll 数学推导,公式
- Codeforces 545D Queue【贪心+模拟】
- 【CodeForces - 758C Unfair Poll】 暴力 + 模拟
- Codeforces 363C Fixing Typos 贪心+字符串模拟
- CodeForces 758C Unfair Poll(模拟)
- Lucene打分公式的数学推导
- Codeforces 724C Ray Tracing(模拟)
- Codeforces-754B-Ilya and tic-tac-toe game(简单模拟判断)
- 理解BP算法-(公式受虐狂,请近,不过这个推导有些错误,有空,我再改过来)
- JMP指令转换公式推导
- CodeForces 724|模拟|贪心|扩展欧几里得|
- CodeForces 615E——Hexagons(二分,模拟)
- 今天竟然忘记了余弦公式和点积公式的互相推导关系了。
- 隐马尔科夫(HMM)的一些公式推导