cf702D Road to Post Office
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.
这题让我火冒三丈
一个人要走d的距离,可以走路也可以开车,走路时每走1单位长度用b时间,开车1单位长度用a时间,保证a<b。但是开车经过k的距离就会抛锚,要修t时间。可以弃车走完剩下的路,问最小时间。
a<b,所以坐车一定比走路优。显然车子的第一个k是不开白不开的。
考虑行进k单位长度,开车用ka+t,走路就是kb。原来以为只要比完看看哪个更优然后一直用下去就好了,结果一看样例1不对,明明能开两段只开了一段,而且更优
然后我就想啊,如果要开车,前几段的路程k时间都是ka+t,最后一段是ka,因为最后弃车就不修了,但是t是他给的,如果t和ka差很多,是不是不开完车子能走的路程而在走到k*某个i的时候停下来会更优呢
然后我就天真的以为这肯定是个单峰的然后要找到i
然后就写了三分。。
最后一看挖槽是贪心
说白了就三种情况:
一是全用ka+t
二是全用db
三是只用一段ka然后剩下的(d-k)b
玛德
感情我看的样例一恰好是第三种情况
艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹这是我当时的心情艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹
这里放的还是我的三分,虽然贪心也是一样的效果
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void write(LL a)
{
if (a<){printf("-");a=-a;}
if (a>=)write(a/);
putchar(a%+'');
}
inline void writeln(LL a){write(a);printf("\n");}
LL d,k,a,b,t,l,r;
inline LL calc(LL x)
{
return x*k*a+(x-)*t+(d-x*k)*b;
}
inline LL sanfen(LL l,LL r)
{
if (r==l)return calc(l);
if (r==l+)
{
LL a=calc(l);
LL b=calc(l+);
return min(a,b);
}
if (r==l+)
{
LL a=calc(l);
LL b=calc(l+);
LL c=calc(l+);
return min(min(a,b),c);
}
LL midl=l+(r-l)/;
LL midr=r-(r-l)/;
LL a=calc(l);
LL b=calc(midl);
LL c=calc(midr);
LL d=calc(r);
if (a<=b)return sanfen(l,midl);
if (b<=c)return sanfen(midl,midr);
return sanfen(midr,r);
}
int main()
{
d=read();k=read();a=read();b=read();t=read();
if(k>=d){printf("%lld\n",a*d);return ;}
l=;r=d/k;
LL ss=d*a+r*t;
printf("%lld\n",min(ss,sanfen(l,r)));
}
cf702D
cf702D Road to Post Office的更多相关文章
- D. Road to Post Office 解析(思維)
Codeforce 702 D. Road to Post Office 解析(思維) 今天我們來看看CF702D 題目連結 題目 略,請直接看原題. 前言 原本想說會不會也是要列式子解或者二分搜,沒 ...
- Educational Codeforces Round 15 Road to Post Office
Road to Post Office 题意: 一个人要从0走到d,可以坐车走k米,之后车就会坏,你可以修或不修,修要花t时间,坐车单位距离花费a时间,走路单位距离花费b时间,问到d的最短时间. 题解 ...
- Codeforces Educational Codeforces Round 15 D. Road to Post Office
D. Road to Post Office time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Educational Codeforces Round 15_D. Road to Post Office
D. Road to Post Office time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Educational Codeforces Round 15 D. Road to Post Office 数学
D. Road to Post Office time limit per test 1 second memory limit per test 256 megabytes input standa ...
- codeforces 702D D. Road to Post Office(数学)
题目链接: D. Road to Post Office time limit per test 1 second memory limit per test 256 megabytes input ...
- Codeforces 702 D Road to Post Office
题目描述 Vasiliy has a car and he wants to get from home to the post office. The distance which he needs ...
- codeforce 702D Road to Post Office 物理计算路程题
http://codeforces.com/contest/702 题意:人到邮局去,距离d,汽车在出故障前能跑k,汽车1公里耗时a,人每公里耗时b,修理汽车时间t,问到达终点最短时间 思路:计算车和 ...
- CodeForces 702D Road to Post Office
答案的来源不外乎于3种情况: 纯粹走路,用时记为${t_1}$:纯粹乘车,用时记为${t_2}$:乘车一定距离,然后走路,用时记为${t_3}$. 但是${t_1}$显然不可能成为最优解. 前两个时间 ...
随机推荐
- 2016年11月2日——jQuery源码学习笔记
1.jQuery()函数,即$().有四种不同的调用方式. (1)传递CSS选择器(字符串)给$()方法,返回当前文档中匹配该选择器的元素集.可选第二个参数,一个元素或jQuery对象,定义元素查询的 ...
- Adb工具常用操作-转(二)
一. PC与模拟器或真机交换文件(adb pull和adb push) 在开发阶段或其他原因,经常需要将PC上的文件复制到模拟器或真机上,或将模拟机和真机上的文件复制到PC上.使用adb pull和a ...
- OpenWrt的主Makefile工作过程
OpenWrt是一个典型的嵌入式Linux工程,了解OpenWrt的Makefile的工作过程对提高嵌入式Linux工程的开发能力有极其重要意义. OpenWrt的主Makefile文件只有100行, ...
- Java 获取字符串中第N次出现的字符位置
public static int getCharacterPosition(String string){ //这里是获取"/"符号的位置 Matcher slash ...
- power desinger 学习笔记<六>
原帖地址:http://blog.csdn.net/spt110/article/details/8640849 PowerDesigner中Table视图同时显示Code和Name,像下图这样的效果 ...
- 苹果开发 笔记(80)升级IOS 9 和 XCode 7 引起的问题记录
原文: http://blog.csdn.net/hero82748274/article/details/48629461 问题一: 升级xcode 7最低的系统配置要求 升级了ios9 后使用 x ...
- javascript基础学习(十)
javascript之数组 学习要点: 数组的介绍 定义数组 数组元素 数组的方法 一.数组的介绍 数组中的元素类型可以是数字型.字符串型.布尔型等,甚至也可以是一个数组. 二.定义数组 1.通过数组 ...
- mysql如何将一个表导出为excel表格
方法一:进入到mysql的控制台,输入: 1. SELECT * INTO OUTFILE ‘./test.xls‘ FROM tb1 WHERE 1 ORDER BY id DESC LIMIT ...
- 如何学习YII
我是在Yii的官方wiki上看到这篇文章的.读的第一遍觉得很不错,还有一种想翻译出来的冲动.虽然,本人英文很烂,但是毕竟写了这样多年的代码,估计大概的意思是能有的吧.英文原文:http://www.y ...
- JqGrid自定义toolbar
1.设置toolbar参数为[true,"top"],其意思是toolbar显示在Grid顶部,且其id为t_+Grid的id.e.g.: Grid的id为myGrid,toolb ...