• cf702D Road to Post Office


    D. Road to Post Office
    time limit per test 1 second
    memory limit per test 256 megabytes
    input standard input
    output standard output

    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 ≤ 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.
    Output

    Print the minimal time after which Vasiliy will be able to reach the post office.

    Examples
    input
    5 2 1 4 10
    output
    14
    input
    5 2 1 4 5
    output
    13
    Note

    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

    玛德

    感情我看的样例一恰好是第三种情况

    艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹这是我当时的心情艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹艹

    这里放的还是我的三分,虽然贪心也是一样的效果

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<algorithm>
     6 #include<cmath>
     7 #include<ctime>
     8 #define LL long long
     9 #define inf 0x7ffffff
    10 #define pa pair<int,int>
    11 #define pi 3.1415926535897932384626433832795028841971
    12 using namespace std;
    13 inline LL read()
    14 {
    15     LL x=0,f=1;char ch=getchar();
    16     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    17     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    18     return x*f;
    19 }
    20 inline void write(LL a)
    21 {
    22     if (a<0){printf("-");a=-a;}
    23     if (a>=10)write(a/10);
    24     putchar(a%10+'0');
    25 }
    26 inline void writeln(LL a){write(a);printf("
    ");}
    27 LL d,k,a,b,t,l,r;
    28 inline LL calc(LL x)
    29 {
    30     return x*k*a+(x-1)*t+(d-x*k)*b;
    31 }
    32 inline LL sanfen(LL l,LL r)
    33 {
    34     if (r==l)return calc(l);
    35     if (r==l+1)
    36     {
    37         LL a=calc(l);
    38         LL b=calc(l+1);
    39         return min(a,b);
    40     }
    41     if (r==l+2)
    42     {
    43         LL a=calc(l);
    44         LL b=calc(l+1);
    45         LL c=calc(l+2);
    46         return min(min(a,b),c);
    47     }
    48     LL midl=l+(r-l)/3;
    49     LL midr=r-(r-l)/3;
    50     LL a=calc(l);
    51     LL b=calc(midl);
    52     LL c=calc(midr);
    53     LL d=calc(r);
    54     if (a<=b)return sanfen(l,midl);
    55     if (b<=c)return sanfen(midl,midr);
    56     return sanfen(midr,r);
    57 }
    58 int main()
    59 {
    60     d=read();k=read();a=read();b=read();t=read();
    61     if(k>=d){printf("%lld
    ",a*d);return 0;}
    62     l=1;r=d/k;
    63     LL ss=d*a+r*t;
    64     printf("%lld
    ",min(ss,sanfen(l,r)));
    65 }
    cf702D
    ——by zhber,转载请注明来源
  • 相关阅读:
    算法大佬推荐
    学习的两个docker指令
    ie兼容问题解决记录
    缓存函数,curry与偏函数
    uni-app开发时遇到的注意点
    let,var,const区别
    递归的简单理解
    宏任务和微任务的进一步理解
    简单实现一个观察者模式
    业余时间
  • 原文地址:https://www.cnblogs.com/zhber/p/5742961.html
Copyright © 2020-2023  润新知