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
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$就可以了,模拟一遍就好。
1 //It is made by Awson on 2017.9.29 2 #include <set> 3 #include <map> 4 #include <cmath> 5 #include <ctime> 6 #include <queue> 7 #include <stack> 8 #include <vector> 9 #include <cstdio> 10 #include <string> 11 #include <cstring> 12 #include <cstdlib> 13 #include <iostream> 14 #include <algorithm> 15 #define LL long long 16 #define Max(a, b) ((a) > (b) ? (a) : (b)) 17 #define Min(a, b) ((a) < (b) ? (a) : (b)) 18 #define sqr(x) ((x)*(x)) 19 #define lowbit(x) ((x)&(-(x))) 20 using namespace std; 21 void read(int &x) { 22 char ch; bool flag = 0; 23 for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == ‘-‘)) || 1); ch = getchar()); 24 for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar()); 25 x *= 1-2*flag; 26 } 27 28 int a, b, f, k; 29 30 void work() { 31 read(a), read(b), read(f), read(k); 32 if (k==1 && (b < f || b < a-f)) { 33 printf("-1\n"); 34 return; 35 } 36 if (k==2 && (b < f || b < 2*(a-f))) { 37 printf("-1\n"); 38 return; 39 } 40 if (k>2 && (b < 2*f || b < 2*(a-f))) { 41 printf("-1\n"); 42 return; 43 } 44 int now = b-f, ans = 0; 45 bool towards = 0; 46 for (int i = 1; i <= k; i++) { 47 int cost; 48 if (!towards) cost = a-f; 49 else cost = f; 50 if (i!=k) cost *= 2; 51 if (now >= cost) now -= cost; 52 else ans++, now = b-cost; 53 towards = !towards; 54 } 55 printf("%d\n", ans); 56 } 57 int main() { 58 work(); 59 return 0; 60 }