• [Codeforces] #436 C. Bus


    C. Bus

    time limit per test 2 seconds
    memory limit per test 256 megabytes
    input standard input
    output standard output
     

    A bus moves along the coordinate line Ox from the point x = 0 to the point x = a. After starting from the point x = 0, it reaches the point x = a, immediately turns back and then moves to the point x = 0. After returning to the point x = 0 it immediately goes back to the pointx = a and so on. Thus, the bus moves from x = 0 to x = a and back. Moving from the point x = 0 to x = a or from the point x = a to x = 0is called a bus journey. In total, the bus must make k journeys.

    The petrol tank of the bus can hold b liters of gasoline. To pass a single unit of distance the bus needs to spend exactly one liter of gasoline. The bus starts its first journey with a full petrol tank.

    There is a gas station in point x = f. This point is between points x = 0 and x = a. There are no other gas stations on the bus route. While passing by a gas station in either direction the bus can stop and completely refuel its tank. Thus, after stopping to refuel the tank will contain b liters of gasoline.

    What is the minimum number of times the bus needs to refuel at the point x = f to make k journeys? The first journey starts in the pointx = 0.

    Input

    The first line contains four integers abfk (0 < f < a ≤ 106, 1 ≤ b ≤ 109, 1 ≤ k ≤ 104) — the endpoint of the first bus journey, the capacity of the fuel tank of the bus, the point where the gas station is located, and the required number of journeys.

    Output

    Print the minimum number of times the bus needs to refuel to make k journeys. If it is impossible for the bus to make k journeys, print-1.

    Examples
    input
    6 9 2 4
    output
    4
    input
    6 10 2 4
    output
    2
    input
    6 5 4 3
    output
    -1
    Note

    In the first example the bus needs to refuel during each journey.

    In the second example the bus can pass 10 units of distance without refueling. So the bus makes the whole first journey, passes 4 units of the distance of the second journey and arrives at the point with the gas station. Then it can refuel its tank, finish the second journey and pass 2 units of distance from the third journey. In this case, it will again arrive at the point with the gas station. Further, he can refill the tank up to 10 liters to finish the third journey and ride all the way of the fourth journey. At the end of the journey the tank will be empty.

    In the third example the bus can not make all 3 journeys because if it refuels during the second journey, the tanks will contain only 5liters of gasoline, but the bus needs to pass 8 units of distance until next refueling.

     Analysis

    公交车= =

    存在这样一条数轴:被称为Ox

    (所以我们知道坐标系可能被叫做Oxy

    公交车要干的事情,就是从 0 跑到 a 

    然后再跑回来

    根据题意 “Moving from the point x = 0 to x = a or from the point x = a to x = 0 is called a bus journey.

    我们知道单程就是一次Journey

    公交车总共要跑 k 次单程

    而在 f 有一个油井(假装是油井 =w= 才够霸气)

    公交车可以选择加满油或者不加

    加满就是 b 升了

    由于只有这一个油井,所以如果公交车需要预判一下未来的形势,决定要不要加油

    最后输出公交车需要加油的最少次数(节约能源 =w= )

    -------------------------------------------------------

    由于数据比较小,可以直接暴力模拟

    如果公交车还需要走回来的话,在油井的时候需要预判剩下的油够不够走回油井,否则预判能否到达终点即可

    =w= 越简单写起来越容易错,,

    Code

     1 #include<cstdio>
     2 #include<iostream>
     3 using namespace std;
     4 
     5 int a,b,f,k,ans,tank;
     6 
     7 int main(){
     8     scanf("%d%d%d%d",&a,&b,&f,&k);
     9     
    10     tank = b;
    11     
    12     for(int i = 1;i <= k;i++){
    13         if(i%2){ //0 -> a
    14             if(tank < f){ printf("-1"); return 0; }
    15             
    16             tank -= f;
    17             
    18             // Refruel
    19             if(tank < ((i < k)?(2*(a-f)):(a-f))){
    20 //                printf("#%d: %d
    ",k,tank);
    21                 tank = b,ans++;
    22             }
    23             
    24             
    25             
    26             // Can't arr
    27             if(tank < (a-f)){ printf("-1"); return 0; }
    28             
    29             tank -= (a-f);
    30         }else{   //a -> 0
    31             // Can't refruel?
    32             if(tank < (a-f)){ printf("-1"); return 0; }
    33 //            
    34             tank -= (a-f);
    35             
    36             if(tank < ((i < k)?(2*f):(f))){
    37 //                printf("#%d: %d
    ",k,tank);
    38                 tank = b,ans++;
    39             }
    40             
    41             if(tank < f){ printf("-1"); return 0; }
    42             
    43             tank -= f;
    44         }
    45     }
    46     
    47     printf("%d",ans);
    48     
    49     return 0;
    50 }
    C. Bus
  • 相关阅读:
    Android Logcat使用技巧
    SqlClient使用存储过程并获取输出参数的指
    内存调试的东西D/dalvikvm( 809 ): GC_CONCURRENT freed
    android 读写sd卡的权限设置
    Android中EditText的使用方法持續更新
    RSL编译方式的FLEX站点出现#2046错误
    从网络读取数据并动态的显示在ListView中
    LEFT JOIN INNER JOIN 效率比较
    使用Installshield2009 在IIS6部署Asp.net mvc 应用程序
    Installshield2009中使用osql.exe执行数据库脚本
  • 原文地址:https://www.cnblogs.com/Chorolop/p/7596310.html
Copyright © 2020-2023  润新知