这道题,按道理来说是签到题,但是我硬是卡在了test30过不去。
解法就是直接模拟过程,油不够就加。
说一下需要考虑的几个情况:
1.k=1时,油箱容量需要大于 f 和 (a-f)
2.k=2时,油箱容量需要大于 f 和 2(a-f)
3.k>2时,油箱容量需要大于 2f 和 2(a-f)
4.然后将k==1的情况挑出来单独讨论即可
然鹅以上这些点我都注意到了,但是还是忽略了一点...
当k==1,b>=a时,无需加油(我就是卡在这里,只写了b==a无需加油,忽略b>=a的情况。。属实很蠢)。
以下是正解代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <queue>
using namespace std;
int main()
{
int a,b,f,k;
int tmp=0,time=0;
cin>>a>>b>>f>>k;
if(k==1)
{
if(!(b>=f&&b>=(a-f)))
{
cout<<"-1"<<endl;
return 0;
}
}
else if(k==2)
{
if(!(b>=f&&b>=2*(a-f)))
{
cout<<"-1"<<endl;
return 0;
}
}
else
{
if(!(b>=2*f&&b>=2*(a-f)))
{
cout<<"-1"<<endl;
return 0;
}
}
if(k==1&&a<=b)
{
printf("0\n");
return 0;
}
else if(k==1&&b<a)
{
printf("1\n");
return 0;
}
else
{
tmp=b-f;
while(1)
{
if(tmp>=(2*(a-f)))
tmp-=(2*(a-f));
else
{
tmp=b-2*(a-f);
time++;
}
k--;
if(k==1)
{
if(tmp<f)
time++;
break;
}
if(tmp>=(2*f))
tmp-=(2*f);
else
{
tmp=b-2*f;
time++;
}
k--;
if(k==1)
{
if(tmp<(a-f))
time++;
break;
}
}
}
cout<<time<<endl;
return 0;
}