Codeforces Round #386 (Div. 2)C. Tram(模拟+思维)

原创 2017年01月03日 22:07:02

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 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.

Input

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.

Output

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

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 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.
题意:
在一段长为s的路上,你在x1,目的地是x2,有一辆电车以每米t1秒的速度在往复行驶,你的速度为每米t2秒。现在电车在P,方向为d,问最短时间从x1到x2.
题解:
很明显,如果t1>t2,我们就不搭乘电车,直接步行,否则,我们取min(搭车和步行的)。这里需要分析。如果搭乘是最优解,则人移不移动是无所谓的,我们只需要求出电车从p点经过x1到x2的时间即可。
代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll n,ss,tt;
ll t1,t2;
ll pos,d,output;
void keyi()
{
    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()
{
    cin>>n>>ss>>tt;
    cin>>t1>>t2;
    cin>>pos>>d;
        if(t1<t2)
        {
            output=abs(ss-tt)*t2;
            keyi();
            cout<<output<<endl;
        }
        else
        {
            output=abs(ss-tt)*t2;
         cout<<output<<endl;
        }
}
版权声明:加油 加油 加油^-^

Codeforces Round #406 (Div. 2):C. Berzerk(记忆化搜索解决博弈问题)

C. Berzerk time limit per test 4 seconds memory limit per test 256 megabytes input standard in...
  • Jaihk662
  • Jaihk662
  • 2017年03月24日 15:23
  • 812

【Codeforces Round 375 (Div 2) E】【欧拉回路Fleury算法 或网络流】One-Way Reform 每条边定向使得最多的点满足入度=出度

E. One-Way Reform time limit per test 2 seconds memory limit per test 256 megabytes ...
  • snowy_smile
  • snowy_smile
  • 2016年10月04日 10:20
  • 740

Codeforces 432C Prime Swaps【筛法素数预处理+贪心】

C. Prime Swaps time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu...
  • mengxiang000000
  • mengxiang000000
  • 2016年10月11日 21:40
  • 418

LibreOJ β Round #2 A. 模拟只会猜题意【前缀和】

A. 模拟只会猜题意 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 转到题库提交记录返回比赛 ...
  • mengxiang000000
  • mengxiang000000
  • 2017年07月03日 17:56
  • 456

codeforces 776C Molly's Chemicals(连续子序列和为k的次方的个数)

C. Molly's Chemicals time limit per test 2.5 seconds memory limit per test 512 megabytes input...
  • zwj1452267376
  • zwj1452267376
  • 2017年02月24日 02:47
  • 872

Codeforces Round #408 (Div. 2)-C. Bank Hacking-(三种方法)分类讨论,二分,集合

补题速度太慢了,这样可不行啊。 代码里都有解释。 看别人代码有三种写法 set,分类,和二分。 这是个是用分类思想写的,#include using namespace std; /* 一开...
  • qq_35781950
  • qq_35781950
  • 2017年04月19日 20:52
  • 141

数学推公式——较难——Codeforces Round #187 (Div. 2)

题目链接: http://codeforces.com/contest/315/problem/C C. Sereja and Contest time limi...
  • CS_liuqing
  • CS_liuqing
  • 2013年06月16日 15:34
  • 2003

Codeforces Round #244 (Div. 2)(强连通分量,后缀数组)

A. Police Recruits #include #include #include #include using namespace std; int n; int main() { ...
  • u010660276
  • u010660276
  • 2014年05月23日 20:08
  • 425

Codeforces Round #248 (Div. 2) B题 【数据结构:树状数组】

题目链接:http://codeforces.com/contest/433/problem/B 题目大意:给n(1 ≤ n ≤ 105)个数据(1 ≤ vi ≤ 109),其中有m(1 ≤ m ≤...
  • u013912596
  • u013912596
  • 2014年05月24日 18:18
  • 710

Codeforces Round #312 (Div. 2) (第三题是位运算,好题)

比赛:点击打开链接 558A - Lala Land and Apple Trees 分析:从0坐标分开,负半轴一个数组,正半轴一个数组,来记录果树的左边和数量,可以用结构体数组来存储数据,其...
  • loveyou11111111
  • loveyou11111111
  • 2015年08月31日 16:30
  • 162
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Codeforces Round #386 (Div. 2)C. Tram(模拟+思维)
举报原因:
原因补充:

(最多只允许输入30个字)