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\n",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\n",sum);
    }
    return 0;
}
 
 

猜你喜欢

转载自www.cnblogs.com/jianqiao123/p/11237977.html