此文章可以使用目录功能哟↑(点击上方[+])
比赛链接→Educational Codeforces Round 15
Codeforces Problem 702D Road to Post Office
Accept: 0 Submit: 0
Time Limit: 1 second Memory Limit : 256 megabytes
Problem Description
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 ≤ 10^12; 1 ≤ k, a, b, t ≤ 10^6; 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.
Sample Input
5 2 1 4 5
Sample Output
13
Hint
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.
Problem Idea
解题思路:
【题意】
Vasiliy有一辆车,他想从家出发到距离为d的邮局去
Vasiliy的车每行驶k公里,就需要t秒时间修复才能继续行驶
另外我们知道车行驶1公里需要a秒,步行1公里需要b秒(a<b)
问vasiliy最少需要多少时间到达邮局,题目允许vasiliy丢弃汽车步行至终点
【类型】
分类讨论
【分析】
以上是题目的大致流程
可以看出,整个过程可以分为三部分:⒈最初k公里,不用修理(蓝线部分);⒉中间修理t秒,行驶k公里(紫线部分);⒊最终修理t秒,行驶d%k公里(红线部分)
对于最初的k公里,因为不用修理,且a<b,显然我们是要选择开车的,这也就对应第①种情况:d<=k,全程开车直接到,用时为a*d
而中间部分,主要还是得看开车和步行,哪种用时短选哪种,开车时间:t+a*k;步行时间:b*k
当且仅当t+a*k>b*k时,选择步行
这样是不是一定能够最优呢?我们知道的是至少中间这部分这样判断是最优的
那么在中间部分达到最优的条件下,最终部分是否也能够到达最优呢?
假设t+a*k>b*k成立,最终部分的开车时间:t+d%k*a;步行时间:d%k*b
得证,若中间部分步行优于开车时,最终部分步行必定优于开车;反之不然
于是,情况②也得以解决
情况③是最终部分步行,此时t+d%k*a>d%k*b
情况④为全程开车,但我们要算出车修了几次
【时间复杂度&&优化】
O(1)
题目链接→Codeforces Problem 702D Road to Post Office
Source Code
/*Sherlock and Watson and Adler*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = 1005;
const int M = 100005;
const int inf = 1000000007;
const int mod = 1000000007;
int main()
{
__int64 d,k,a,b,t;
scanf("%I64d%I64d%I64d%I64d%I64d",&d,&k,&a,&b,&t);
if(d<=k)//情况①:全程开车,无修理
printf("%I64d\n",a*d);
else if(t+a*k>b*k)//情况②:最初开车,之后一直步行
printf("%I64d\n",a*k+b*(d-k));
else if(t+d%k*a>d%k*b)//情况③:之前一直开车,最终步行
printf("%I64d\n",a*(d-d%k)+((d-d%k)/k-1)*t+d%k*b);
else//情况④:全程开车,含修理,无步行
printf("%I64d\n",a*d+(d-1)/k*t);
return 0;
}
菜鸟成长记