zoukankan      html  css  js  c++  java
  • Codeforces Round #386 (Div. 2) C. Tram

    C. Tram
    time limit per test1 second
    memory limit per test256 megabytes
    inputstandard input
    outputstandard 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的数轴上行走,以每米t1秒的速度从0走到s,再从s回头走到0,这样往复循环的跑。一个人,在x1点,他要到x2点(x1,x2不是同一个点),人行走的速度为每米t2秒。当人和火车在同一时间走到相同的坐标时,人可以搭乘火车。d表示火车当前方向,d=1表示从0走到s,d=-1表示从s走到0。现在,输入数轴长度s,人的出发点x1,目的地x2,火车速度t1,人速度t2,火车初始位置p,和火车初始方向d,问人达到目的地的最短时间是多少。

    题解:要明确一点是,人若搭乘火车,那么人到达目的地的时间即是火车速度经过人后到达目的地所用时间,否则,人徒步走到目的地才是人的速度到达目的地的时间。不会存在什么先以人的速度行走后用火车的速度行驶的复合型走法。因此,直接根据火车的方向和火车与人的相对位置,分类讨论。算出火车接到人再去目的地的所用时间再与人徒步走去的时间取最小值即是答案。
    当火车正向时:
    ①0–>火车–>出发点–>目的地–>s:路程=目的地坐标-火车初始坐标
    ②0–>出发点–>目的地–>火车–>s:路程=总路程-火车初始坐标+总路程+目的地坐标
    ③0–>出发点–>火车–>目的地–>s:路程=总路程-火车初始坐标+总路程+目的地坐标
    ④0–>目的地–>火车–>出发点–>s:路程=总路程-火车初始坐标+总路程-目的地坐标
    ⑤0–>目的地–>出发点–>火车–>s:路程=总路程-火车初始坐标+总路程-目的地坐标
    ⑥0–>火车–>目的地–>出发点–>s:路程=总路程-火车初始坐标+总路程-目的地坐标
    火车逆向时:
    ①0<–火车<–出发点<–目的地<–s:路程=目的地坐标+火车初始坐标
    ②0<–出发点<–目的地<–火车<–s:路程=目的地坐标+火车初始坐标
    ③0<–出发点<–火车<–目的地<–s:路程=目的地坐标+火车初始坐标
    ④0<–目的地<–火车<–出发点<–s:路程=总路程+总路程+火车初始坐标-目的地坐标
    ⑤0<–目的地<–出发点<–火车<–s:路程=火车初始坐标-目的地坐标
    ⑥0<–火车<–目的地<–出发点<–s:路程=总路程+总路程+火车初始坐标-目的地坐标

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<stdlib.h>
    using namespace std;
    int main()
    {
        int s,x1,x2,t1,t2,p,d,i;
        while(scanf("%d%d%d%d%d%d%d",&s,&x1,&x2,&t1,&t2,&p,&d)!=EOF)
        {
           int peo=abs(x2-x1)*t2;
           if(d>0)
           {
               if(p<=x1&&x1<x2)s=x2-p;
               else if(x1<x2&&x2<=p)s=s-p+s+x2;
               else if(x1<p&&p<=x2)s=s-p+s+x2;
               else if(x2<=p&&p<=x1)s=s-p+s-x2;
               else if(x2<x1&&x1<=p)s=s-p+s-x2;
               else if(p<=x2&&x2<x1)s=s-p+s-x2;
           }
           else
           {
               if(p<=x1&&x1<x2)s=p+x2;
               else if(x1<x2&&x2<=p)s=p+x2;
               else if(x1<=p&&p<=x2)s=p+x2;
               else if(x2<=p&&p<x1)s=p+s+s-x2;
               else if(x2<x1&&x1<=p)s=p-x2;
               else if(p<=x2&&x2<x1)s=p+s+s-x2;
           }
           int ans=min(peo,s*t1);
           printf("%d
    ",ans);
        }
    }
    
  • 相关阅读:
    LeetCode 226. Invert Binary Tree
    LeetCode 221. Maximal Square
    LeetCode 217. Contains Duplicate
    LeetCode 206. Reverse Linked List
    LeetCode 213. House Robber II
    LeetCode 198. House Robber
    LeetCode 188. Best Time to Buy and Sell Stock IV (stock problem)
    LeetCode 171. Excel Sheet Column Number
    LeetCode 169. Majority Element
    运维工程师常见面试题
  • 原文地址:https://www.cnblogs.com/kuronekonano/p/11794331.html
Copyright © 2011-2022 走看看