您的位置:首页 > 其它

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]

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: