Codeforces 702D Road to Post Office(模拟 + 公式推导)

题目链接:http://codeforces.com/problemset/problem/702/D

题意:

  一个人要去邮局取东西,从家到达邮局的距离为 d, 它可以选择步行或者开车,车每走 k 公里就要花费 t秒修一次才可以继续开,车每公里花费 a秒,步行每公里花费 b秒。依次给出d, k, a, b, t。问最少需要花费多少时间到达邮局。车刚开始时是好的。

思路:

  首先可以模拟一下,可以想到,如果车速比步速块,那么前 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,那么此时直接比较车速和步速,哪个快选择哪个就好了。

代码:

 1 #include <iostream>
 2 #include <cmath>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <cstdlib>
 6 #include <stack>
 7 #include <queue>
 8 #include <vector>
 9 #include <algorithm>
10 #include <string>
11 #define mearv(a, b) memset(a, b, sizeof(a))
12 #define mestrc(a, b, c) memset(&(a), b, sizeof(c))
13 
14 typedef long long LL;
15 using namespace std;
16 const int MAXN = 100000;
17 const LL INF = 1e15;
18 
19 int main() {
20     LL d, k, a, b, t;
21     scanf("%I64d%I64d%I64d%I64d%I64d", &d, &k, &a, &b, &t);
22     LL ans = 0;
23     if(a < b) {
24         if(d > k) ans += k * a, d -= k;//此时可以把路程分为两段。
25         else ans += d * a, d = 0;//总路程小于乘一次车的路程,则直接开车走完全程
26     }
27     else ans += d * b, d = 0;//b步速大于车速,步行走完全程
28     if(d > 0) {
29         LL othr = d % k, car = d / k;
30         if(car * (t + k * a) < car * k * b) ans += car * (t + k * a); //前car * k 公里的选择
31         else ans += car * k * b;
32         if(othr * b < t + othr * a) ans += othr * b; //后 othr公里的选择
33         else ans += t + othr * a;
34     } 
35     printf("%I64d
", ans);
36     return 0;
37 }
原文地址:https://www.cnblogs.com/Ash-ly/p/5725992.html