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

来源:互联网 发布:软件模型 编辑:程序博客网 时间:2024/06/04 20:06

题意:有一个小车,在[ 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;}

原创粉丝点击