您的位置:首页 > 其它

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

2017-10-04 09:36 477 查看

http://codeforces.com/contest/864/problem/C

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.

Sample Input

Input

6 9 2 4

Output

4

Input

6 10 2 4

Output

2

Input

6 5 4 3

Output

-1

Hint

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.

题意:

有一段路,从0——a,在f处有一个加油站;有一辆车,有容量为bL的油箱,1L有走一个单位距离。问走k次最少需要加油多少次?从0到a是一次,从a到0也是一次。

输入a,b,f,k,输出ans;

思路

由于判断是否需要加油的条件是能不能到达下一次的加油站,所以我们把起点和终点都看做加油站。

首先第一段距离是从0点到加油站,需要初始化一下

然后,加油站到下一个加油站的距离是2*(a-f)和2*f;

最后一段是从加油站到0或终点

一开始模拟的很麻烦,写了100多行,然后在博客上找到了一个用数组的方法很简洁于是分享一下。

CODE

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int a,b,f,k;
int dis[100005];
int main()
{
scanf("%d%d%d%d",&a,&b,&f,&k);
int curpos=0,curgas=b,curg=0;
int ans=0;
dis[0]=f;      //第一段距离,0点到加油站
for(int i=1;i<k;i++)//中间距离,加油站到加油站
if(i%2)
dis[i]=2*(a-f);
else
dis[i]=2*f;
if(k%2)     //最后一段距离,加油站到终点
dis[k]=a-f;
else
dis[k]=f;
for(int i=0;i<=k;i++)
{
if(curgas<dis[i])//无法到达下一个点则退出
{
ans=-1;
break;
}
if(i<k&&dis[i]+dis[i+1]>curgas)//到下一个加油站需要加油
{
curgas=b;
ans++;
}
else if(i<k&&dis[i]+dis[i+1]<=curgas)//到下一个加油站不需要加油
curgas-=dis[i];
}
cout << ans << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces 模拟