CF 436(DIV.2)c

匿名 (未验证) 提交于 2019-12-03 00:19:01
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 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.

Input

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.

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
Copy
6 9 2 4 
Output
Copy
4 
Input
Copy
6 10 2 4 
Output
Copy
2 
Input
Copy
6 5 4 3 
Output
Copy
-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; }

文章来源: CF 436(DIV.2)c
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!