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.
Examples
input
6 9 2 4
output
4
input
6 10 2 4
output
2
input
6 5 4 3
output
-1
题目大意
公交车从起点0到终点a需要往返k次(往返算两次),油箱容量为b,初始油箱为满,在路途中b位置有一个加油站。问最少加多少次油可以完成任务。
解题思路
模拟,将路程分为三段,起点->加油站,加油站->终点(次数为奇数时要拿出来单独判断这里)->加油站,加油站->起点。
代码实现
#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define ll long long
int main()
{
int a,b,f,k;
while(~scanf("%d %d %d %d",&a,&b,&f,&k))
{
int t=b;
int countt=0;
int num=0;
bool flag=true;
while(countt<k)
{
if(t<f)
{
printf("-1\n");
flag=false;
break;
}
else
{
t-=f;
if(k%2==1&&k-countt==1)
{
if(t>=a-f)
{
printf("%d\n",num);
flag=false;
break;
}
else if(b>=a-f)
{
printf("%d\n",num+1);
flag=false;
break;
}
else if(b<a-f)
{
printf("-1\n");
flag=false;
break;
}
}
else
{
if(b<2*(a-f))
{
printf("-1\n");
flag=false;
break;
}
else if(t>=2*(a-f))
{
t-=2*(a-f);
}
else
{
num++;
t=b;
t-=2*(a-f);
}
}
if(k%2==0&&k-countt==2)
{
if(t>=f)
{
printf("%d\n",num);
flag=false;
break;
}
else if(b>=f)
{
printf("%d\n",num+1);
flag=false;
break;
}
else if(b<f)
{
printf("-1\n");
flag=false;
break;
}
}
else
{
if(b<2*f)
{
printf("-1\n");
flag=false;
break;
}
else if(t>=2*f)
{
t-=f;
}
else
{
num++;
t=b;
t-=f;
}
}
}
countt+=2;
}
if(flag)
printf("%d\n",num);
}
return 0;
}