Codeforces 864C Bus(模拟)

C. Bus
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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.

题意:

一辆车,需要反复地来往两地,从x=0(起点)到x=a(终点)称为一段旅程,反过来也是一段旅程。距离起点f km的路上有一个加油站,汽车只能在这里加满油。问汽车完成k段旅程需要多少加几次油?

题解:

直接模拟就好了。真的很简单。哎,当初看这个题的时候一直在考虑最优解,然而考虑了很多情况,最终什么也没做出来,真的明明是乱搞一下就OK了。败军之将不足言勇,我真是个智障T_T,还是要接着刷水题。

#include<iostream>
using namespace std;
const int maxn=1e4+5;
int res[maxn];
int main()
{
	int a,b,f,k;
	while(cin>>a>>b>>f>>k)
	{
		int cnt=0;
		int lo=2*f,so=2*(a-f);
		int t=(k-1)/2;
		res[cnt++]=f;
		if(k&1)
		{
			
			for(int i=0;i<t;i++)
			{
				res[cnt++]=so;
				res[cnt++]=lo;
			}
			res[cnt++]=a-f;
		} 
		else 
		{
			res[cnt++]=so;
			for(int i=0;i<t;i++)
			{
				res[cnt++]=lo;
				res[cnt++]=so;
			}
			res[cnt++]=f;
		}
		int ans=0;
		if(b<f||b<a-f)
			cout<<"-1"<<endl;
		else
		{
			int temp=b;
			bool flag=false;
			for(int i=0;i<cnt-1;i++)
			{
				if(res[i]>b)
				{
					flag=true;
					break;
				}
				if(res[i]+res[i+1]<=temp)
				{
					temp-=res[i];
				}
				else
				{
					ans++;
					temp=b;
				}
			}
			if(flag)
				cout<<"-1"<<endl;
			else
				cout<<ans<<endl;
		}
	}
	return 0;
}
原文地址:https://www.cnblogs.com/orion7/p/7630856.html