本文主要是介绍I - Bus CodeForces - 864C(追车问题),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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
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 4 units 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 5 liters of gasoline, but the bus needs to pass 8 units of distance until next refueling.
题意: 给出跑多少个轮回和加油站位置,最少加多少次油
思路: 追车问题有很多种,本题是直接贪心能不加油就不加油。
但本题还可以提前算出每一次加油的位置,然后在当前加油站位置时,看能否不需要加油,定义dp[i]:在第i次加油站位置出发时需要的最小加油次数,那么状态转移就是找到最大的那个不需要加油下一个加油站位置j,得到dp[i] = dp[j] + 1,表示从i出发的最小加油次数等于到了j加了次油再出发的最小加油次数。
(orz叉姐dp代码)。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>using namespace std;int main()
{int a,b,f,k;scanf("%d%d%d%d",&a,&b,&f,&k);if(b < f || b < (a - f) || (k > 1 && b < 2 * (a - f)) || (k > 2 && b < 2 * f)){puts("-1");return 0;}int ans = 0;int num = b;for(int i = 1;i <= k;i++){if(i == k && num >= a)break;if(i % 2){if(num < 2 * a - f){ans++;num = b - (a - f);}else{num -= a;}}else{if(num < a + f){ans++;num = b - f;}elsenum -= a;}}printf("%d\n",ans);return 0;
}
叉姐dp
#include <cstdio>
#include <vector>int main()
{int a, b, f_, k;scanf("%d%d%d%d", &a, &b, &f_, &k);long long f = f_;long long x = a;std::vector<long long> p { 0LL, f };for (int i = 1; i < k; ++ i) {f = 2 * x - f;p.push_back(f);x += a;}p.push_back(x);std::vector<int> dp(p.size());for (int i = p.size() - 2, j = (int)p.size() - 1; i >= 0; -- i) {while (p[j] - p[i] > b) {j --;}if (i == j) {puts("-1");return 0;}dp[i] = dp[j] + 1;}printf("%d\n", dp[0] - 1);
}
这篇关于I - Bus CodeForces - 864C(追车问题)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!