codeforces 864C - Bus

http://codeforces.com/problemset/problem/864/C




给出起点与终点的距离,在这之中有一个加油站,汽车油箱有限,问最少需要加油多少次,能够在起点与终点之间来回K次,按照题意模拟,但是需要很多细节,主要思路就是每次判断到达最远的点之后反向能不能到加油站,如果能则在去的路上就不需要加油了。






#include<bits/stdc++.h>
using namespace std;
int main(){
	long long int a,b,f,k;
	while(cin>>a>>b>>f>>k)
	{
		bool dir=0;
		int bb=b;
		int ans=0;
		int tim=0;
		while(1)
		{
			if(ans==k)break;
			if(!dir)
			{
				dir=!dir;
				if(ans==k-1)
				{
					if(b>=a)
					{
						ans++;
						continue;
					}
					else if(b>=f&&bb>=a-f)
					{
						tim++;
						ans++;
						continue;
					}
					else {
						break;
					}
				}
				if(b>=a+a-f)
				{
					b-=a;
					ans++;
				}
				else if(b>=f&&bb>=a-f)
				{
					b=bb-(a-f);
					tim++;
					ans++;
				}
				else{
					break;
				}
			}
			else
			{
				dir=!dir;
				if(ans==k-1)
				{
					if(b>=a)
					{
						ans++;
						continue;
					}
					else if(b>=a-f&&bb>=f)
					{
						tim++;
						ans++;
						continue;
					}
					else {
						break;
					}
				}
				if(b>=a+f)
				{
					b-=a;
					ans++;
				}
				else if(b>=a-f&&bb>=f)
				{
					b=bb-f;
					tim++;
					ans++;
				}
				else break;
			 } 
		}
		if(ans==k)
		{
			cout<<tim<<endl;
		}
		else cout<<"-1"<<endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值