codeforces 746C 模拟

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 sx1 and x2 (2 ≤ s ≤ 1000, 0 ≤ x1, x2 ≤ sx1 ≠ 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 3meters, 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.

题意     0到s线段中给出两个点x1和x2,从x1到x2,然后可以坐车,车开始在p点车头方向为d(d==1向右 d==-1向左),

汽车在0-S之间不停地开,到达0或S点会掉头,人可以在任意时间上、下车,汽车每t1秒走1,步行每t2秒走1,问人从x1到x2的最短时间。

官方解析  

It is easy to show that if Igor faster than the tram the answer is |x1 - x2|·t2.

In the other case we need to use the following hint: the time of arrive does not depend on how much Igor walk before enter the tram, if the tram will reach the finish point faster than Igor. So Igor can wait the tram in the point x1.

The answer is minimum of the following values: the time during which Igor will reach the point x2 by foot and the time during which the tram will reach at first the point x1 and than the point x2.

 这要比较汽车先经过x1再经过x2所花的时间和人从x1走到x2所花的时就可以了,然后分类讨论前者的情况 共有六种情况(注意合并后)。

AC代码

 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <string.h>
 4 #include <stdlib.h>
 5 #include <iostream>
 6 #include <sstream>
 7 #include <algorithm>
 8 #include <string>
 9 #include <queue>
10 #include <vector>
11 using namespace std;
12 const int maxn= 2005;
13 const int maxm= 1e9+10;
14 const int inf = 0x3f3f3f3f;
15 typedef long long ll;
16 int main()
17 {
18     int s,x1,x2,t1,t2,d,p;
19     scanf("%d %d %d",&s,&x1,&x2);
20     scanf("%d %d",&t1,&t2);
21     scanf("%d %d",&p,&d);
22     int ans1,ans2;
23     ans1=abs(x1-x2)*t2;
24     if(d==1)              //向右三种
25     {
26         if(p<=x1&&x2>x1)
27             ans2=(x2-p)*t1;
28         else if(x2>x1)
29             ans2=(s-p+s+x2)*t1;
30         else if(x2<x1)
31             ans2=(s-p+s-x2)*t1;
32     }
33     else                //向左三种
34     {
35         if(p>=x1&&x2<x1)
36             ans2=(p-x2)*t1;
37         else if(x2<x1)
38             ans2=(p+s+s-x2)*t1;
39         else if(x2>x1)
40             ans2=(p+x2)*t1;
41     }
42     printf("%d
",min(ans1,ans2));  //取两者最小
43 }
原文地址:https://www.cnblogs.com/stranger-/p/7677988.html