zoukankan      html  css  js  c++  java
  • A. New Building for SIS Codeforce

    You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.

    The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building.

    The picture illustrates the first example.

    You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.

    Input

    The first line of the input contains following integers:

    • n: the number of towers in the building (1 ≤ n ≤ 108),
    • h: the number of floors in each tower (1 ≤ h ≤ 108),
    • a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h),
    • k: total number of queries (1 ≤ k ≤ 104).

    Next k lines contain description of the queries. Each description consists of four integers tafatbfb (1 ≤ ta, tb ≤ n1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.

    Output

    For each query print a single integer: the minimum walking time between the locations in minutes.

    Example
    input
    Copy
    3 6 2 3 3
    1 2 1 3
    1 4 3 4
    1 2 2 3
    output
    Copy
    1
    4
    2
    一道特别简单的题
    题意:从一个塔的某一层到另一个塔的某一层,需要的最短时间。条件:每上下一层都要一秒,每从一个塔去临近的塔需要一秒,每个塔去临近的塔只有a-b层有通道去。 
    思路:分类讨论一下。 
    1、当出发地和目的地是同一个塔:abs(fa-fb) 
    2.1、当出发地和目的地不同塔:但是有一个在有通道范围内,或者两者都不在范围内但一个<=a一个>=b:abs(ta-tb)+abs(fa-fb) 
    2.2、当出发和目的地楼层都<=a:abs(ta-tb)+abs(fa-a)+abs(fb-a) 
    2.3、当出发地和目的地楼层都>=b:abs(ta-tb)+abs(fa-b)+abs(fb-b)
    此人的思维:https://blog.csdn.net/miranda_ymz/article/details/81603271
    但是我不喜欢此人的代码。
    if(ta==tb)
                cout<<abs(fa-fb)<<endl;
    View Code

    这是第一部分“当出发地和目的地是同一个塔”

    else
            {
                if(fa>=b&&fb>=b)
                    cout<<abs(ta-tb)+(fb-b)+(fa-b)<<endl;
                else if(fa<=a&&fb<=a)
                    cout<<abs(ta-tb)+(a-fa)+(a-fb)<<endl;
                else
                    cout<<abs(ta-tb)+abs(fa-fb)<<endl;
            }
    View Code

    这是第二部分,你只需要控制先判断同在b之上和同在a之下,剩余的就是一个大于b或者小于a,另一个在a和b之间。(不用那么多的判断)

    上代码

     1 #include<stdio.h>
     2 #include<iostream>
     3 #include<algorithm>
     4 using namespace std;
     5 int main()
     6 {
     7     int n,h,a,b,k;
     8     cin>>n>>h>>a>>b>>k;
     9     while(k--)
    10     {
    11         int ta,tb,fa,fb;
    12         cin>>ta>>fa>>tb>>fb;
    13         if(ta==tb)
    14             cout<<abs(fa-fb)<<endl;
    15         else
    16         {
    17             if(fa>=b&&fb>=b)
    18                 cout<<abs(ta-tb)+(fb-b)+(fa-b)<<endl;
    19             else if(fa<=a&&fb<=a)
    20                 cout<<abs(ta-tb)+(a-fa)+(a-fb)<<endl;
    21             else
    22                 cout<<abs(ta-tb)+abs(fa-fb)<<endl;
    23         }
    24     }
    25     return 0;
    26 }
    View Code
  • 相关阅读:
    [干货,阅后进BAT不是梦]面试心得与总结---BAT、网易、蘑菇街
    [干货,阅后进BAT不是梦]面试心得与总结---BAT、网易、蘑菇街
    JSP九大内置对象,七大动作,三大指令
    JSP九大内置对象,七大动作,三大指令
    Java异常处理
    Java异常处理
    java和C和C++关系
    c++中指针常量,常指针,指向常量的常指针区分
    c++中指针常量,常指针,指向常量的常指针区分
    Method and apparatus for verification of coherence for shared cache components in a system verification environment
  • 原文地址:https://www.cnblogs.com/luhongkai/p/9509789.html
Copyright © 2011-2022 走看看