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;
}
相关文章
|
7天前
|
云安全 人工智能 自然语言处理
|
11天前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
990 35
|
5天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
667 4
|
7天前
|
机器学习/深度学习 人工智能 数据可视化
1秒生图!6B参数如何“以小博大”生成超真实图像?
Z-Image是6B参数开源图像生成模型,仅需16GB显存即可生成媲美百亿级模型的超真实图像,支持中英双语文本渲染与智能编辑,登顶Hugging Face趋势榜,首日下载破50万。
527 25
|
14天前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
859 59
Meta SAM3开源:让图像分割,听懂你的话
|
4天前
|
弹性计算 网络协议 Linux
阿里云ECS云服务器详细新手购买流程步骤(图文详解)
新手怎么购买阿里云服务器ECS?今天出一期阿里云服务器ECS自定义购买流程:图文全解析,阿里云服务器ECS购买流程图解,自定义购买ECS的设置选项是最复杂的,以自定义购买云服务器ECS为例,包括付费类型、地域、网络及可用区、实例、镜像、系统盘、数据盘、公网IP、安全组及登录凭证详细设置教程:
195 114
|
11天前
|
人工智能 前端开发 算法
大厂CIO独家分享:AI如何重塑开发者未来十年
在 AI 时代,若你还在紧盯代码量、执着于全栈工程师的招聘,或者仅凭技术贡献率来评判价值,执着于业务提效的比例而忽略产研价值,你很可能已经被所谓的“常识”困住了脚步。
576 50
大厂CIO独家分享:AI如何重塑开发者未来十年
|
7天前
|
存储 自然语言处理 测试技术
一行代码,让 Elasticsearch 集群瞬间雪崩——5000W 数据压测下的性能避坑全攻略
本文深入剖析 Elasticsearch 中模糊查询的三大陷阱及性能优化方案。通过5000 万级数据量下做了高压测试,用真实数据复刻事故现场,助力开发者规避“查询雪崩”,为您的业务保驾护航。
382 25