Codeforces Round #436 (Div. 2) C. Bus

Description

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

题目大意

公交车从起点0到终点a需要往返k次(往返算两次),油箱容量为b,初始油箱为满,在路途中b位置有一个加油站。问最少加多少次油可以完成任务。

解题思路

模拟,将路程分为三段,起点->加油站,加油站->终点(次数为奇数时要拿出来单独判断这里)->加油站,加油站->起点。

代码实现

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define ll long long
int main()
{
    int a,b,f,k;
    while(~scanf("%d %d %d %d",&a,&b,&f,&k))
    {
        int t=b;
        int countt=0;
        int num=0;
        bool flag=true;
        while(countt<k)
        {
            if(t<f)
            {
                printf("-1\n");
                flag=false;
                break;
            }
            else
            {
                t-=f;
                if(k%2==1&&k-countt==1)
                {
                    if(t>=a-f)
                    {
                        printf("%d\n",num);
                        flag=false;
                        break;
                    }
                    else if(b>=a-f)
                    {
                        printf("%d\n",num+1);
                        flag=false;
                        break;
                    }
                    else if(b<a-f)
                    {
                        printf("-1\n");
                        flag=false;
                        break;
                    }
                }
                else
                {
                    if(b<2*(a-f))
                    {
                        printf("-1\n");
                        flag=false;
                        break;
                    }
                    else if(t>=2*(a-f))
                    {
                        t-=2*(a-f);
                    }
                    else
                    {
                        num++;
                        t=b;
                        t-=2*(a-f);
                    }
                }
                if(k%2==0&&k-countt==2)
                {
                    if(t>=f)
                    {
                        printf("%d\n",num);
                        flag=false;
                        break;
                    }
                    else if(b>=f)
                    {
                        printf("%d\n",num+1);
                        flag=false;
                        break;
                    }
                    else if(b<f)
                    {
                        printf("-1\n");
                        flag=false;
                        break;
                    }
                }
                else
                {
                    if(b<2*f)
                    {
                        printf("-1\n");
                        flag=false;
                        break;
                    }
                    else if(t>=2*f)
                    {
                        t-=f;
                    }
                    else
                    {
                        num++;
                        t=b;
                        t-=f;
                    }
                }
            }
            countt+=2;
        }
        if(flag)
            printf("%d\n",num);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值