[Codeforces 864C]Bus

时间:2021-08-29 20:44:38

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 abfk (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

6 9 2 4

Sample Output

4

HINT

In the first example the bus needs to refuel during each journey.

题解

$a$是终点位置,$b$是油箱容量,$f$是加油站位置,$k$是要遍历的次数 $0$ ~ $a$或$a$ ~ $0$ 都算一次。

贪心策略是能不加油就不加油。我们记录在第$i$次遍历时在加油站不加油的油量$now$就可以了,模拟一遍就好。

 //It is made by Awson on 2017.9.29
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define LL long long
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define sqr(x) ((x)*(x))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
void read(int &x) {
char ch; bool flag = ;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || ); ch = getchar());
for (x = ; isdigit(ch); x = (x<<)+(x<<)+ch-, ch = getchar());
x *= -*flag;
} int a, b, f, k; void work() {
read(a), read(b), read(f), read(k);
if (k== && (b < f || b < a-f)) {
printf("-1\n");
return;
}
if (k== && (b < f || b < *(a-f))) {
printf("-1\n");
return;
}
if (k> && (b < *f || b < *(a-f))) {
printf("-1\n");
return;
}
int now = b-f, ans = ;
bool towards = ;
for (int i = ; i <= k; i++) {
int cost;
if (!towards) cost = a-f;
else cost = f;
if (i!=k) cost *= ;
if (now >= cost) now -= cost;
else ans++, now = b-cost;
towards = !towards;
}
printf("%d\n", ans);
}
int main() {
work();
return ;
}