1 second
256 megabytes
standard input
standard output
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 points x = 0 and x = s.
Igor is at the point x1. He should reach the point x2. Igor passes 1 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 point x1.
Igor can enter the tram unlimited number of times at any moment when his and the tram's positions coincide. It is not 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 than 1 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 and t2 (1 ≤ t1, t2 ≤ 1000) — the time in seconds in which the tram passes 1 meter and the time in seconds in which Igor passes 1 meter.
The third line contains two integers p and d (1 ≤ p ≤ s - 1, d is either 1 or ) — the position of the tram in the moment Igor came to the point x1 and the direction of the tram at this moment. If , the tram goes in the direction from the point s to the point 0. If d = 1, the tram goes in the direction from the point 0 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 pass 2 meters and it takes 8 seconds in total, because he passes 1 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 in 1 second. Thus, Igor will reach the point x2 in 7 seconds in total.
/*
D题Debug调了20分钟有点炸,C题才没出。但是好在有惊无险的上分了(维持稳定的上分节奏,非常好)
分开求,一种是人的不坐车,另一种就是坐车,而坐车的时候实际就是求车走的时间
*/
#include<bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
int main(){
//freopen("in.txt","r",stdin);
int s,x1,x2,t1,t2,p,d1,d2;
scanf("%d%d%d%d%d%d%d",&s,&x1,&x2,
&t1,&t2,
&p,&d1);
//车速比人慢直接没有坐车的必要
//if(t1>=t2){
// printf("%d\n",abs(x1-x2)*t1);
// return 0;
//}
//剩下的只是车比人快
/*
情况1:不坐车
*/
int time1=abs(x1-x2)*t2;
/*
情况2:坐车(实际就是求车到达x2的时间
*/
if(x1-x2<) d2=;
else d2=-;
int step=;//模拟车走过的米数
int x=p;//记录车的位置
int flag1=,flag2=;//记录车是不是从x1开到x2的
if(x==x1) flag1=;
if(flag1&&x==x2) flag2=;//必须是从x1开过来的并且开到了x2车才能记录下来
while(true){
//cout<<x<<endl;
if(flag2) break;
if(x==&&d1==-) d1=;//掉头
if(x==s&&d1==) d1=-;//掉头
x+=d1;
step++;
if(x==x1) flag1=;
if(flag1&&x==x2) flag2=;//必须是从x1开过来的并且开到了x2车才能记录下来
}
int time2=t1*step;
//cout<<time1<<" "<<time2<<endl;
printf("%d\n",min(time1,time2));
return ;
}