Codeforces 864C Round#436 C :果然还是模拟最难

题意:有一个小车,在[ 0 , a ]范围内做周期运动。运动轨迹是 0->a->0->a->0->a……,走一趟定义为0->a或者a->0。中间有个加油站,在p的位置。然后小车的油箱容量是b,走一单位的距离就消耗一单位的油。小车路过加油站可以选择不加油,也可以选择把油加满。请问要走k趟,最少需要加几次油。


题解:模拟吧 模拟吧。。。。不说了。。。心塞了。。。atleft记录小车现在是不是在左端点处。tot记录现在走了多少趟了。ans记录加了多少次油。。。然后请大力模拟。


Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL a,bb,b,f,k,c1,c2,rest,cc;
int main(){
	cin>>a>>b>>f>>k;
	LL c1 = f;
	LL c2 = a-f;
	LL ans =0;
	LL tot =0;
	LL rest = b;
	if (c1>b||c2>b){
		cout<<-1<<endl;
		return 0;
	}
	bool atleft =true;
	while (tot<k){
//		cout<<tot<<" "<<atleft<<" "<<rest<<" "<<ans<<endl;
		LL round = rest/a;
		if (tot+round>=k){
			break;
		}
		if (round*a==rest){
			round--;
		}
		LL temp = rest-round*a;
		if (round<0){
			cout<<-1<<endl;
			return 0;
		}
		if (atleft){
			if (round&1){
				if (c2>temp){
					temp+=a;
					round--;
				}
			}else{
				if (c1>temp){
					temp+=a;
					round--;
				}
			}
		}else{
			if (round&1){
				if (c1>temp){
					temp+=a;
					round--;
				}
			}else{
				if (c2>temp){
					temp+=a;
					round--;
				}
			}
		}
		
		if (round<0){
			cout<<-1<<endl;
			return 0;
		}
		tot+=round;
		if (tot>=k){
			break;
		}
		if (atleft){
			if (round&1){
				rest = b-c1;
				tot++;
				ans++;
			}else{
				rest = b-c2;
				tot++;
				ans++;
				atleft = false;
			}
		}else{
			if (round&1){
				rest = b-c2;
				tot++;
				ans++;
			}else{
				rest = b-c1;
				tot++;
				ans++;
				atleft = true;
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值