一个长度为S的隧道,0为最左边,S为最右边,电车是一直在动的,每t1s走1m,如果撞倒了边界,就会马上掉头行驶不会停下来,人是随意选择行走方式的(可以逆向,可以停下来,怎么走都行),每t2s走1m.这个人初始位子在x1.想要到位子x2处,问他从x1到x2的最短时间.人上车必须在整数米处才可以上车。
思路:
1、我们分类讨论,如果人的速度比车子的速度快,那么很明显我们直接走过去就行了。
2、如果人的速度没有车子的速度快,那么我们人只要停在原地等着车过来就行了,因为无论我们是逆行去找车,还是顺着走等车,其实都是在等车,而且某些情况我们走了之后还要等车子多走几圈才能接到人,那么我们不如原地等着车过来即可。
那么我们此时就要比较:
①人直接从x1走到x2的时间
②车子经过x1之后再到x2的时间
两者的大小,取最小即为答案。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll __int64
ll n,ss,tt;
ll t1,t2;
ll pos,d,output;
void init()
{
int flag=0;
ll tim=0;
while(1)
{
if(pos==ss)
{
if(flag==0)flag=1;
}
if(pos==tt)
{
if(flag==1)
{
output=min(output,tim);
break;
}
}
if(pos==n&&d==1||pos==0&&d==-1)
{
d=-d;
pos+=d;
tim+=t1;
}
else
{
pos+=d;
tim+=t1;
}
}
}
int main()
{
while(~scanf("%I64d%I64d%I64d",&n,&ss,&tt))
{
scanf("%I64d%I64d",&t1,&t2);
scanf("%I64d%I64d",&pos,&d);
if(t1<t2)
{
output=abs(ss-tt)*t2;
init();
printf("%I64d\n",output);
}
else
{
output=abs(ss-tt)*t2;
printf("%I64d\n",output);
}
}
}