您的位置:首页 > 其它

codeforces 864C - Bus

2017-09-29 21:46 330 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: