• Codeforces Gym 101808J


    J. Saeed and Folan

    time limit per test

    1.0 s

    memory limit per test

    256 MB

    input

    standard input

    output

    standard output

    Saeed is waiting for Shahhoud who is late as usual. To kill time, Saeed decided to walk around the hallway. The hallway can be seen as a straight line with 109 tiles numbered from 1 to 109 from left to right.

    Saeed starts his walk at the p1th tile facing either left or right. Each second he moves one tile in the direction he is facing. The boundaries of his walk are L1 and R1 (L1 < R1). This means that if he is walking left and reaches the L1th tile, he changes his direction and starts walking right, And if he is walking right and reaches the R1th tile, he changes his direction and starts moving left. (no time is needed to change direction)

    Folan is also walking in a similar way, except that he starts at the p2th tile and the boundaries of his walk are L2 and R2 (L2 < R2).

    Saeed hates Folan, and he is wondering how many times he will meet Folan at the same tile at the same moment within K seconds of walking (the direction of walking does not matter), can you help him?

    Input

    The first line contains a single integer T, the number of test cases.

    Each test case consists of 3 lines, the first line contains 4 space-separated integers L1, R1, p1, and D1 (if D1 is 0 then Saeed is facing left, and if D1 is 1 then Saeed is facing right).

    The second line also contains 4 space-separated integers L2, R2, p2 and D2, the information of Folan's walk (similar to Saeed's).

    1 ≤ L1 < R1 ≤ 109 and p1 is in the range [L1, R1].

    1 ≤ L2 < R2 ≤ 109 and p2 is in the range [L2, R2].

    1 ≤ K ≤ 1000

    p1 ≠ p2

    Output

    For each test case, print one line containing one integer, the number of times Saeed meets Folan on the same tile at the same time.

    Example

    input

    Copy

    2
    1 2 1 1
    1 2 2 0
    1
    2 5 3 0
    1 2 1 1
    4

    output

    Copy

    0
    1
    这题挺有意思的,我以下面第一种方式提交,次次都是WA,以下面第二种方式提交就AC,才知道自己对if-else的使用都有误区。。。
    代码一(WA):
    #include <iostream>
    #include <cstdio>
    using namespace std;
    int main(){
        int l1,r1,p1,d1;
        int l2,r2,p2,d2;
        int k;
        int t;
        int sum;
        cin>>t;
        while(t--){
            cin>>l1>>r1>>p1>>d1;
            cin>>l2>>r2>>p2>>d2;
            cin>>k;
            sum=0;
            int a=p1;
            int b=p2;
            while(k--){
                if(d1==0){
                    if(a>l1) a--;
                    else if(a==l1){
                        a++;
                        d1=1;
                    }
                }
                if(d1==1) {
                    if(a<r1) a++;
                    else if(a==r1){
                        a--;
                        d1=0;
                    }
                }
                if(d2==0){
                    if(b>l2) b--;
                    else if(b==l2){
                        b++;
                        d2=1;
                    }
                }
                if(d2==1) {
                    if(b<r2) b++;
                    else if(b==r2){
                        b--;
                        d2=0;
                    }
                }
                if(a==b) sum++;
            }
            printf("%d
    ",sum);
        }
        return 0;
    }
    代码二(AC):
    #include <iostream>
    #include <cstdio>
    using namespace std;
    int main(){
        int l1,r1,p1,d1;
        int l2,r2,p2,d2;
        int k;
        int t;
        int sum;
        cin>>t;
        while(t--){
            cin>>l1>>r1>>p1>>d1;
            cin>>l2>>r2>>p2>>d2;
            cin>>k;
            sum=0;
            int a=p1;
            int b=p2;
            while(k--){
                if(d1==0){
                    if(a>l1) a--;
                    else if(a==l1){
                        a++;
                        d1=1;
                    }
                }
                else {
                    if(a<r1) a++;
                    else if(a==r1){
                        a--;
                        d1=0;
                    }
                }
                if(d2==0){
                    if(b>l2) b--;
                    else if(b==l2){
                        b++;
                        d2=1;
                    }
                }
                else {
                    if(b<r2) b++;
                    else if(b==r2){
                        b--;
                        d2=0;
                    }
                }
                if(a==b) sum++;
            }
            printf("%d
    ",sum);
        }
        return 0;
    }
     
     
    天晴了,起飞吧
  • 相关阅读:
    汽车行业如何利用大数据
    汽车行业如何利用大数据
    Linux下Gcc 的编译过程
    第一个GraphX程序
    VC++ 模拟&quot;CLICK事件&quot;关闭指定窗体
    基于Canvas的Char.js库使用
    VELT-0.1.6开发:载入根文件系统
    HDU 5411 CRB and puzzle (Dp + 矩阵高速幂)
    JavaScript图片裁剪
    C++ string中的几个小陷阱,你掉进过吗?
  • 原文地址:https://www.cnblogs.com/jianqiao123/p/11237977.html
Copyright © 2020-2023  润新知