B. Kind Anton

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Once again, Boris needs the help of Anton in creating a task. This time Anton needs to solve the following problem:

There are two arrays of integers aa and bb of length nn. It turned out that array aa contains only elements from the set {1,0,1}{−1,0,1}.

Anton can perform the following sequence of operations any number of times:

  1. Choose any pair of indexes (i,j)(i,j) such that 1i<jn1≤i<j≤n. It is possible to choose the same pair (i,j)(i,j) more than once.
  2. Add aiai to ajaj. In other words, jj-th element of the array becomes equal to ai+ajai+aj.

For example, if you are given array [1,1,0][1,−1,0], you can transform it only to [1,1,1][1,−1,−1], [1,0,0][1,0,0] and [1,1,1][1,−1,1] by one operation.

Anton wants to predict if it is possible to apply some number (zero or more) of these operations to the array aa so that it becomes equal to array bb. Can you help him?

Input

Each test contains multiple test cases.

The first line contains the number of test cases tt (1t100001≤t≤10000). The description of the test cases follows.

The first line of each test case contains a single integer nn (1n1051≤n≤105)  — the length of arrays.

The second line of each test case contains nn integers a1,a2,,ana1,a2,…,an (1ai1−1≤ai≤1)  — elements of array aa. There can be duplicates among elements.

The third line of each test case contains nn integers b1,b2,,bnb1,b2,…,bn (109bi109−109≤bi≤109)  — elements of array bb. There can be duplicates among elements.

It is guaranteed that the sum of nn over all test cases doesn't exceed 105105.

Output

For each test case, output one line containing "YES" if it's possible to make arrays aa and bb equal by performing the described operations, or "NO" if it's impossible.

You can print each letter in any case (upper or lower).

Example
input
Copy
5
3
1 -1 0
1 1 -2
3
0 1 1
0 2 2
2
1 0
1 41
2
-1 0
-1 -41
5
0 1 -1 1 -1
1 1 -1 1 -1
output
Copy
YES
NO
YES
YES
NO
Note

In the first test-case we can choose (i,j)=(2,3)(i,j)=(2,3) twice and after that choose (i,j)=(1,2)(i,j)=(1,2) twice too. These operations will transform [1,1,0][1,1,2][1,1,2][1,−1,0]→[1,−1,−2]→[1,1,−2]

In the second test case we can't make equal numbers on the second position.

In the third test case we can choose (i,j)=(1,2)(i,j)=(1,2) 4141 times. The same about the fourth test case.

In the last lest case, it is impossible to make array aa equal to the array b.

 

题意:给定n个数为数组a,其中只能包含-1,0,1,再给出数组b1<=i<j<=n 可以aj变成ai+aj;

次数不限,就是说后面的数可以由前面的数通过一定次数的累加得到,问a能否通过这样的变换变为b

思路;利用map来统计,倒着遍历同时减掉,这样就知道前面是否存在1-1了。

代码:

#include<iostream>

#include<cstdio>

#include<cstring>

#include<cstdlib>

#include<cmath>

#include<algorithm>

#include<set>

#include<stack>

#include<queue>

#include<map>

#include<vector>

using namespace std;

typedef long long ll;

ll a[100005];

ll b[100005];

int main()

{

    int t;

    cin>>t;

    while(t--)

    {

        int n;

        cin>>n;

        map<int,int> p;  // 记录前面是否有需要的这个数

        for(int i=0;i<n;i++)

        {

            cin>>a[i];

            p[a[i]]++;  // 让a[i]个数++

        }

 

        for(int i=0;i<n;i++)

            cin>>b[i];

        int flag=1;

 

        for(int i=n-1;i>=0;i--)  // 倒着遍历

        {

            p[a[i]]--; //不算自己和后面的,这些减掉

            if(a[i]>b[i])  // 需要前面有-1

            {

                if(p[-1]>0)

                    continue;

                else

                {

                    flag=0;

                    break;

                }

            }

            else if(a[i]<b[i])  // 需要前面有1

            {

                if(p[1]>0)

                    continue;

                else

                {

                    flag=0;

                    break;

                }

            }

        }

        if(flag==1)

           cout<<"YES"<<endl;

        else

            cout<<"NO"<<endl;

        p.clear();

 

 

    }

 

    return 0;

}

共同努力,加油!

posted @ 2020-04-09 21:53  牛哄哄的柯南  阅读(288)  评论(0编辑  收藏  举报