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;
}
相关文章
|
5天前
|
存储 弹性计算 人工智能
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
2025年9月24日,阿里云弹性计算团队多位产品、技术专家及服务器团队技术专家共同在【2025云栖大会】现场带来了《通用计算产品发布与行业实践》的专场论坛,本论坛聚焦弹性计算多款通用算力产品发布。同时,ECS云服务器安全能力、资源售卖模式、计算AI助手等用户体验关键环节也宣布升级,让用云更简单、更智能。海尔三翼鸟云服务负责人刘建锋先生作为特邀嘉宾,莅临现场分享了关于阿里云ECS g9i推动AIoT平台的场景落地实践。
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
|
3天前
|
云安全 人工智能 自然语言处理
阿里云x硅基流动:AI安全护栏助力构建可信模型生态
阿里云AI安全护栏:大模型的“智能过滤系统”。
|
4天前
|
人工智能 自然语言处理 自动驾驶
关于举办首届全国大学生“启真问智”人工智能模型&智能体大赛决赛的通知
关于举办首届全国大学生“启真问智”人工智能模型&智能体大赛决赛的通知
|
7天前
|
存储 机器学习/深度学习 人工智能
大模型微调技术:LoRA原理与实践
本文深入解析大语言模型微调中的关键技术——低秩自适应(LoRA)。通过分析全参数微调的计算瓶颈,详细阐述LoRA的数学原理、实现机制和优势特点。文章包含完整的PyTorch实现代码、性能对比实验以及实际应用场景,为开发者提供高效微调大模型的实践指南。
623 2
|
5天前
|
JavaScript API 开发工具
如何在原生App中调用Uniapp的原生功能?
如何在原生App中调用Uniapp的原生功能?
311 139
|
4天前
|
编解码 自然语言处理 文字识别
Qwen3-VL再添丁!4B/8B Dense模型开源,更轻量,仍强大
凌晨,Qwen3-VL系列再添新成员——Dense架构的Qwen3-VL-8B、Qwen3-VL-4B 模型,本地部署友好,并完整保留了Qwen3-VL的全部表现,评测指标表现优秀。
400 7
Qwen3-VL再添丁!4B/8B Dense模型开源,更轻量,仍强大