CF 436(DIV.2)c

13 篇文章 0 订阅
5 篇文章 0 订阅
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
Copy
6 9 2 4
Output
Copy
4
Input
Copy
6 10 2 4
Output
Copy
2
Input
Copy
6 5 4 3
Output
Copy
-1

题意:

        给你一段路长度为a,要你走k次,在位置f有一个加油站,车一开始有b个单位的油,问你最少要加多少次油。

思路:

        很明显我们可以把车程分类,从左到右和从右到左,然后再分类,最后一次和不是最后一次,经过这2次分类,答案就很明显了。

code:

        

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<queue>
#include<functional>
typedef long long LL;
#define maxn 300005
using namespace std;
vector<int>arr[maxn];
LL n, a, b, k, f,flag,ans,d1,d2,mark,now;
int main()
{
	cin >> a >> b >> f >> k;
	d1 = f, d2 = a - f;
	mark = 1;
	now = b;
	while (k&&!flag)
	{
		if (mark%2)
		{
			if (now < d1)
			{
				flag = 1;
				break;
			}
			else
			{
				now -= d1;
				if (now < 2 * d2&&k!=1)
				{
					if (b < d2)
					{
						flag = 1;
						break;
					}
					else
					{
						now = b - d2;
						ans++;
					}
				}
				else if (k == 1)
				{
					if (now < d2)
					{
						if (b < d2)
						{
							flag = 1;
							break;
						}
						else
						{
							ans++;
						}
					}
				}
				else if (now >= d2*2)
				{
					now -= d2;
				}
			}
		}
		else
		{
			if (now < d2)
			{
				flag = 1;
				break;
			}
			else
			{
				now -= d2;
				if (now < d1 * 2 && k != 1)
				{
					if (b < d1)
					{
						flag = 1;
						break;
					}
					else
					{
						ans++;
						now = b - d1;
					}
				}
				else if (k == 1)
				{
					if (now < d1)
					{
						if (b < d1)
						{
							flag = 1;
							break;
						}
						else
						{
							ans++;
						}
					}
				}
				else if (now >= 2 * d1)
				{
					now -= d1;
				}
			}
		}
		mark++;
		k--;
	}
	if (!flag)
		cout << ans << endl;
	else
		cout << -1 << endl;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值