B. Kind Anton_牛哄哄的柯南

简介: B. Kind Anton

题目链接:http://codeforces.com/contest/1333/problem/B


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:


Choose any pair of indexes (i,j)(i,j) such that 1≤i<j≤n1≤i<j≤n. It is possible to choose the same pair (i,j)(i,j) more than once.

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 (1≤t≤100001≤t≤10000). The description of the test cases follows.


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


The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (−1≤ai≤1−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 (−109≤bi≤109−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,再给出数组b,1<=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;
}
相关文章
|
1月前
|
Perl 容器
【CKA模拟题】学会使用ReplicaSet和Deployment
【CKA模拟题】学会使用ReplicaSet和Deployment
22 1
|
5月前
|
Kubernetes API 数据安全/隐私保护
k8s学习-CKS真题-Pod指定ServiceAccount
k8s学习-CKS真题-Pod指定ServiceAccount
47 0
|
10月前
|
Kubernetes 固态存储 API
【k8s 系列】k8s 学习十八,replicaSet,DaemonSet and Job
上一篇讲到的 ReplicationController 是用于复制和在异常的时候重新调度节点的 K8S 组件,后面 K8S 又引入了 ReplicaSet 资源来替代 ReplicationController
|
5月前
|
JavaScript 前端开发 Java
用代码送上 Happy New Year
用代码送上 Happy New Year
22 0
|
7月前
|
JavaScript 前端开发
01HUI -HUI介绍
01HUI -HUI介绍
24 0
|
7月前
|
JavaScript
27HUI - picker(huiPicker)
27HUI - picker(huiPicker)
27 0
|
数据采集 消息中间件 缓存
牛逼哄哄的 JD-hotkey !
牛逼哄哄的 JD-hotkey !
|
Kubernetes 容器
no matches for kind “Deployment“ in version “extensions/v1beta1“
no matches for kind “Deployment“ in version “extensions/v1beta1“
|
Android开发
【Solve】InnerClass annotations are missing corresponding EnclosingMember annotations. Such InnerClass annotations are ignored
【Solve】InnerClass annotations are missing corresponding EnclosingMember annotations. Such InnerClass annotations are ignored
【Solve】InnerClass annotations are missing corresponding EnclosingMember annotations. Such InnerClass annotations are ignored
|
Go
The New Year: Meeting Friends
The New Year: Meeting Friends
35 0
The New Year: Meeting Friends