Codeforces Round #436 (Div. 2)C. Bus codeforces 864C. Bus

C. Bus
time limit per test
 2 seconds
memory limit per test
 256 megabytes
input
 standard input
output
 standard output

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 pointx = 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 ≤ 1061 ≤ b ≤ 1091 ≤ 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
Note

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

In the second example the bus can pass 10 units of distance without refueling. So the bus makes the whole first journey, passes 4units of the distance of the second journey and arrives at the point with the gas station. Then it can refuel its tank, finish the second journey and pass 2 units of distance from the third journey. In this case, it will again arrive at the point with the gas station. Further, he can refill the tank up to 10 liters to finish the third journey and ride all the way of the fourth journey. At the end of the journey the tank will be empty.

In the third example the bus can not make all 3 journeys because if it refuels during the second journey, the tanks will contain only 5liters of gasoline, but the bus needs to pass 8 units of distance until next refueling.

题意:有一辆车在x=0与x=a之间来回走动,从x=0到x=a算一次路程,从x=a到x=0也算一次路程。现在给你k次路程,起始点在x=0,此时的车是满油的,在这段路中间有一个加油站位置为x=f,这辆车的汽油容量为b,每走一格就消耗一单位的油,车可以在中途加油,现在问你完成这k次路程的最小的加油的次数。如果可以完成,那么输出最少的加油次数;如果不能那么就输出-1。

思路:

汽车某刻时刻的油量为s,我们先将这辆车置于加油站处,此时的s=b-f,在开一个从1到k的循环,奇数代表向右跑,偶数反之,最后一次i=k特判。那么我们判断此时的车能不能到达终点再回来,即2*(a-f)与s比较如果不行那么此时一定要加油,返回到达加油站时s=s-2*(a-f)。向左时一样的道理,判断2f与s的关系。如果这过程中s一旦小于0那么退出输出-1,最后一次(就是第k次)需要单独判断,根据奇偶性判断此时的车的移向,不考虑往返直接考虑此时能不能到达终点即可。(2 1 1 1 这个数据卡了很多人)

代码:

[cpp]  view plain  copy
  1. #include<iostream>  
  2. #include<cmath>  
  3. #include<algorithm>  
  4. #include<cstdio>  
  5. #include<cstring>  
  6.   
  7. using namespace std;  
  8. typedef long long ll;  
  9.   
  10. int main()  
  11. {  
  12.     ll a, b, f, k;  
  13.     scanf("%lld%lld%lld%lld", &a, &b, &f, &k);  
  14.    
  15.     ll num = 0, s = b - f;  
  16.     for (int i = 1; i <= k; i++)  
  17.     {  
  18.         if (s<0)  
  19.         {  
  20.             cout << -1 << endl;  
  21.             return 0;  
  22.         }  
  23.         if (i == k)  
  24.         {  
  25.             if (k % 2)  
  26.             {  
  27.                 if (s < a - f)  
  28.                 {  
  29.                     num++;  
  30.                     s = b;  
  31.                 }  
  32.                 if (s < a - f)  
  33.                 {  
  34.                     cout << -1 << endl;  
  35.                     return 0;  
  36.                 }  
  37.             }  
  38.             else  
  39.             {  
  40.                 if (s<f)  
  41.                 {  
  42.                     num++;  
  43.                     s = b;  
  44.                 }  
  45.                 if (s<f)  
  46.                 {  
  47.                     cout << -1 << endl;  
  48.                     return 0;  
  49.                 }  
  50.             }  
  51.         }  
  52.         else  
  53.         {  
  54.             if (i % 2)  
  55.             {  
  56.                 if (2 * (a - f)>s)  
  57.                 {  
  58.                     num++;  
  59.                     s = b - 2 * (a - f);  
  60.                 }  
  61.                 else  
  62.                     s = s - 2 * (a - f);  
  63.             }  
  64.             else  
  65.             {  
  66.                 if (2 * f>s)  
  67.                 {  
  68.                     num++;  
  69.                     s = b - f - f;  
  70.                 }  
  71.                 else  
  72.                 {  
  73.                     s = s - f - f;  
  74.                 }  
  75.             }  
  76.         }  
  77.   
  78.     }  
  79.     printf("%lld\n", num);  
  80. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值