D - Bus

D - Bus
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.

题意:
一条总线沿着坐标线OX从点X=0移动到点X=A。从点X=0开始,到达点X=A,立即返回,然后移动到点X=0。返回到点X=0后,立即返回到点X=A,依此类推。因此,总线从X=0移动到X=A并向后移动。从X=0点移动到X=A点或从X=A点移动到X=0点被称为公共汽车旅行。总的来说,这辆公共汽车必须行驶k次。

公共汽车的油箱能装B升汽油。为了通过一个单位的距离,公共汽车只需要消耗一升汽油。这辆公共汽车第一次行驶时油箱已加满。

在X=F点有一个加油站。该点位于X=0和X=A点之间。公交路线上没有其他加油站。当巴士从任何一个方向经过加油站时,它都可以停下来给油箱加油。因此,停止加油后,油箱将包含B升汽油。

在X=F点,巴士需要加油的最少次数是多少?第一次旅程从点X=0开始。

#include <iostream>

using namespace std;
int n[100000];
int main()
{
    int a,b,f,k;
    cin >> a >> b >> f >> k;
    int sum=b,flag=0;
    int t=0,i;
    n[0]=f;
    for(i=1; i<k; i++)
    {
        if(i%2==0)
        {
            n[i]=2*f;
        }
        else
        {
            n[i]=2*(a-f);
        }
    }
    if(k%2==0)
    {
        n[k]=f;
    }
    else
    {
        n[k]=a-f;
    }
    for(i=0; i<=k; i++)
    {
        if(n[i]>sum)
        {
            flag=1;
            cout << -1;
            break;
        }
        else if(i<k&&n[i]+n[i+1]>sum)
        {
            t++;
            sum=b;
        }
        else if(i<k&&n[i]+n[i+1]<=sum)
        {
            sum=sum-n[i];
        }
    }
    if(flag==0)
    {
        cout << t;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZ --瑞 hopeACMer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值