Codeforces Round #386 (Div. 2)C. Tram

C. Tram
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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 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 pointx2, 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.

Input

The first line contains three integers s,x1 andx2 (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 passes 1 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.

Output

Print the minimum time in seconds which Igor needs to get from the point x1 to the pointx2.

Examples
Input
4 2 4
3 4
1 1
Output
8
Input
5 4 0
1 2
3 1
Output
7
Note

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 point1 in 6 seconds (because he has to pass3 meters, but he passes 1 meters per2 seconds). At that moment the tram will be at the point1, so Igor can enter the tram and pass 1 meter in 1 second. Thus, Igor will reach the pointx2 in7 seconds in total.

题意:lgor在x1,目的地在x2,列车在p位置,方向是d,列车最远能开到s,列车在0~s之间来回开,lgor每走一米耗时t2秒,列出每行驶一米耗时t2秒,问lgor到达目的地的最短时间。

思路:其实只需要知道lgor要不要坐车,不坐就按步行时间算,坐的话其实只要让列车不断地开,知道经过x1点再开往x2,所用的时间就是答案,因为列车是不会中途掉头的,我们也不需要考虑lgor什么时候上车,是停一会还是走一会再上车,因为上下车时间是不算的。这就相当于列车不断地开呀开,开到x2就好了,但是得先经过x1点,这样才能保证列车到x2的时候,lgor在车上,又或者说列车经过x1点的时候肯定在x1行走的同一方向或者相反方向但已经载上lgor了,因为要坐车,就要顺着坐车的规则,最后比较两种方法取最小值。

#include <bits/stdc++.h>
using namespace std;
#define ll long long int

int main()
{
    int s, x1, x2, t1, t2, p, d, r1, r2;
    scanf("%d %d %d", &s, &x1, &x2);
    scanf("%d %d", &t1, &t2);
    scanf("%d %d", &p, &d);
    if(t1 >= t2){
         printf("%d\n", abs(x2-x1)*t2);
         return 0;
    }
    else{
       r1 = abs(x2-x1)*t2;
       r2 = 0;
       bool flag = 0;
       while(1){
           if(p==x1) flag = 1;
           if(flag&&p==x2) break;
           r2 += t1;
           if(p==s) d=-1;
           if(p==0) d=1;
           if(d==-1) p--;
           else p++;
           //if(p==x1) flag = 1;
       }
    }
    printf("%d\n", min(r1, r2));
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值