题目传送门:点击打开链接
The tram in Berland goes along a straight line from the point 0 to the point s and back, passing 1 meter per t1 seconds in both directions. It means that the tram is always in the state of uniform rectilinear motion, instantly turning around at pointsx = 0 and x = s.
Igor is at the point x1. He should reach the pointx2. Igor passes1 meter per t2 seconds.
Your task is to determine the minimum time Igor needs to get from the point x1 to the point x2, if it is known where the tram is and in what direction it goes at the moment Igor comes to the pointx1.
Igor can enter the tram unlimited number of times at any moment when his and the tram's positions coincide. It isnot obligatory that points in which Igor enter and exit the tram are integers. Assume that any boarding and unboarding happens instantly. Igor can move arbitrary along the line (but not faster than1 meter per t2 seconds). He can also stand at some point for some time.
The first line contains three integers s,x1 and x2 (2 ≤ s ≤ 1000,0 ≤ x1, x2 ≤ s,x1 ≠ x2) — the maximum coordinate of the point to which the tram goes, the point Igor is at, and the point he should come to.
The second line contains two integers t1 andt2 (1 ≤ t1, t2 ≤ 1000) — the time in seconds in which the tram passes1 meter and the time in seconds in which Igor passes1 meter.
The third line contains two integers p andd (1 ≤ p ≤ s - 1,d is either 1 or) — the position of the tram in the moment Igor came to the pointx1 and the direction of the tram at this moment. If
, the tram goes in the direction from the points to the point 0. Ifd = 1, the tram goes in the direction from the point0 to the point s.
Print the minimum time in seconds which Igor needs to get from the point x1 to the point x2.
4 2 4 3 4 1 1
8
5 4 0 1 2 3 1
7
In the first example it is profitable for Igor to go by foot and not to wait the tram. Thus, he has to pass2 meters and it takes 8 seconds in total, because he passes1 meter per 4 seconds.
In the second example Igor can, for example, go towards the point x2 and get to the point 1 in 6 seconds (because he has to pass 3 meters, but he passes 1 meters per 2 seconds). At that moment the tram will be at the point 1, so Igor can enter the tram and pass 1 meter in1 second. Thus, Igor will reach the point x2 in 7 seconds in total.
题意:有一条铁轨,这条铁轨长s,每隔一个单位有一个站台。一个人要从x1到x2。这个人的移动速度是t2秒一单位距离。电车的移动速度是t1秒一单位距离。p是电车的初始坐标。d表示电车的初始前进方向:1代表由0驶向S,-1代表从s驶向0。电车在0和s处瞬间掉头。问最少需要多长时间才能从X1到达X2?
思路:要注意人是可以不坐电车的。注意这种情况,然后就可以分很多种情况讨论了。我们可以固定点p。然后讨论x1和x2。要注意x1和x2大小不确定。
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define m(a) memset(a,0,sizeof(a))
int main()
{
int n,x1,x2;
int t1,t2;
int p,d;
while(~scanf("%d%d%d%d%d%d%d",&n,&x1,&x2,&t1,&t2,&p,&d))
{
int len=abs(x2-x1);
int man=len*t2;
int tram=0,ans=0;
if(d==1)
{
if(p<=x1 && x1<x2)
{
tram=x2-p;
}
else if(x1<=p && p<=x2)
{
tram=2*(n-p)+2*p+(x2-p);
}
else if(x1<x2 && x2<=p)
{
tram=2*(n-p)+p+x2;
}
else if(p<=x2 && x2<x1)
{
tram=(n-p)+(n-x2);
}
else if(x2<=p && p<=x1)
{
tram=2*(n-p)+p-x2;
}
else if(x2<x1 && x1<=p)
{
tram=n-p+(n-x2);
}
}
else
{
if(p<=x1 && x1<x2)
{
tram=p+x2;
}
else if(x1<=p && p<=x2)
{
tram=p+x2;
}
else if(x1<x2 && x2<=p)
{
tram=p+x2;
}
else if(p<=x2 && x2<x1)
{
tram=p+n+(n-x2);
}
else if(x2<=p && p<x1)
{
tram=p+n+(n-x2);
}
else if(x2<x1 && x1<=p)
{
tram=p-x2;
}
}
ans = min(tram*t1, man);
printf("%d\n", ans);
}
return 0;
}