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.
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.
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
题意:
给你一段路长度为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;
}