zoukankan      html  css  js  c++  java
  • 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 point x = 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 = 0 is 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 point x = 0.

    Input

    The first line contains four integers a, b, f, k (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 5 liters of gasoline, but the bus needs to pass 8 units of distance until next refueling.

    题目题意就是给你一条路长为a,在f出有加油站,每次加油最多为b升,旅行k次.

    输出所需加油的最小次数.直接模拟就行.

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int a,b,f,k,now,ans=0;
     4 int main(){
     5   cin>>a>>b>>f>>k;
     6   now=b;
     7   if(f>b){
     8     cout<<-1<<endl;
     9     return 0;
    10   }
    11   now-=f;
    12   for(int i=1;i<k;i++){
    13     int d=(i&1)?a-f:f;//如果i为奇数,则取后面一段
    14     d<<=1;
    15     if(d>now)
    16       ans++,now=b;
    17     if(d>now){
    18       cout<<-1<<endl;
    19       return 0;
    20     }
    21     now-=d;
    22   }
    23   int d=(k&1)?a-f:f;
    24   if(d>now)
    25     ans++,now=b;
    26   if(d>now){
    27     cout<<-1<<endl;
    28     return 0;
    29   }
    30   cout<<ans<<endl;
    31   return 0;
    32 }
  • 相关阅读:
    Visual Studio使用阿里云Code Git服务器的常见问题
    使用Quartz.net来执行定时任务
    DirectorySearcher.Filter 属性(转)
    angular2的ElementRef在组件中获取不到
    angular2 ngfor循环
    angular2 日期格式化
    angular2在模板中使用属性引发Cannot read property 'xxx' of undefined
    Java ConcurrentHashMap存入引用对象时也是线程安全的
    FtpHelper实现ftp服务器文件读写操作(C#)
    Window服务项目脚手架
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/7597002.html
Copyright © 2011-2022 走看看