E - Curiosity Has No Limits CodeForces - 1031B(暴力)

B. Curiosity Has No Limits
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
When Masha came to math classes today, she saw two integer sequences of length n−1 on the blackboard. Let’s denote the elements of the first sequence as ai (0≤ai≤3), and the elements of the second sequence as bi (0≤bi≤3).

Masha became interested if or not there is an integer sequence of length n, which elements we will denote as ti (0≤ti≤3), so that for every i (1≤i≤n−1) the following is true:

ai=ti|ti+1 (where | denotes the bitwise OR operation) and
bi=ti&ti+1 (where & denotes the bitwise AND operation).
The question appeared to be too difficult for Masha, so now she asked you to check whether such a sequence ti of length n exists. If it exists, find such a sequence. If there are multiple such sequences, find any of them.

Input
The first line contains a single integer n (2≤n≤105) — the length of the sequence ti.

The second line contains n−1 integers a1,a2,…,an−1 (0≤ai≤3) — the first sequence on the blackboard.

The third line contains n−1 integers b1,b2,…,bn−1 (0≤bi≤3) — the second sequence on the blackboard.

Output
In the first line print “YES” (without quotes), if there is a sequence ti that satisfies the conditions from the statements, and “NO” (without quotes), if there is no such sequence.

If there is such a sequence, on the second line print n integers t1,t2,…,tn (0≤ti≤3) — the sequence that satisfies the statements conditions.

If there are multiple answers, print any of them.

Examples
inputCopy
4
3 3 2
1 2 0
outputCopy
YES
1 3 2 0
inputCopy
3
1 3
3 2
outputCopy
NO
Note
In the first example it’s easy to see that the sequence from output satisfies the given conditions:

t1|t2=(012)|(112)=(112)=3=a1 and t1&t2=(012)&(112)=(012)=1=b1;
t2|t3=(112)|(102)=(112)=3=a2 and t2&t3=(112)&(102)=(102)=2=b2;
t3|t4=(102)|(002)=(102)=2=a3 and t3&t4=(102)&(002)=(002)=0=b3.
In the second example there is no such sequence.

题意:给一个a数组和b数组,让你凑成一个数组t,使得 ti | ti+1 =ai ti&ti+1=bi,如果存在这样的数组t
输出yes,以及t数组,否则输出NO

思路:很显然先暴力枚举前两位,然后接下来的,只要一位一位枚举就可以了,时间复杂度44100000*4 所以不可能超时

#include<bits/stdc++.h>
#define LL long long
#define Max 100005
#define Mod 1e9+7
const LL mod=1e9+7;
const LL inf=0x3f3f3f3f;
using namespace std;
int a[Max],b[Max],n;
int ans[Max];
int main()
{
    scanf("%d",&n);
    int flag=0;
    for(int i=0; i<n-1; i++)
        scanf("%d",&a[i]);
    for(int j=0; j<n-1; j++)
        scanf("%d",&b[j]);
    for(int i=0; i<n; i++)
    {
        ans[i]=inf;
    }
    int i;
    for(i=0; i<=3; i++)
        for(int j=0; j<=3; j++)//暴力枚举前两位
        {
            if((i|j)==a[0] && (i&j)==b[0])//如果符合条件,暴力枚举接下来的
            {
                ans[0]=i;
                ans[1]=j;
               // printf("%d %d\n",ans[0],ans[1]);
                int k;
                for(k=1; k<n-1; k++)//枚举剩下的每一位
                {
                    for(int j=0; j<=3; j++)
                    {
                        if((ans[k]|j)==a[k] && (ans[k]&j)==b[k])
                        {
                            ans[k+1]=j;
                        }
                    }
                    if(ans[k+1]==inf)//如果没有符合条件的直接退出
                        break;
                }
                if(k>=n-1)//如果全部满足条件,直接输出
                {
                    printf("YES\n");
                    for(int i=0; i<n; i++)
                        printf("%d ",ans[i]);
                    return 0;
                }

            }
        }
    printf("NO\n");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值