A bus moves along the coordinate line Ox from the point x to the point xa. After starting from the point x, it reaches the point xa, immediately turns back and then moves to the point x. After returning to the point x it immediately goes back to the point xa and so on. Thus, the bus moves from x to xa and back. Moving from the point x to xa or from the point xa to x 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 xf. This point is between points x and xa. 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 xf to make k journeys? The first journey starts in the point x.
The first line contains four integers a, b, f, k (fa6, b9, k4) ― 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.
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.
6 9 2 4
4
6 10 2 4
2
6 5 4 3
-1
题意:
思路:
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; }