B Curiosity Has No Limits

版权声明:大家一起学习,欢迎转载,转载请注明出处。若有问题,欢迎纠正! https://blog.csdn.net/memory_qianxiao/article/details/83269074

B. Curiosity Has No Limits

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

When Masha came to math classes today, she saw two integer sequences of length n−1n−1 on the blackboard. Let's denote the elements of the first sequence as aiai (0≤ai≤30≤ai≤3), and the elements of the second sequence as bibi (0≤bi≤30≤bi≤3).

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

The question appeared to be too difficult for Masha, so now she asked you to check whether such a sequence titi of length nn 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 nn (2≤n≤1052≤n≤105) — the length of the sequence titi.

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

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

Output

In the first line print "YES" (without quotes), if there is a sequence titi 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 nn integers t1,t2,…,tnt1,t2,…,tn (0≤ti≤30≤ti≤3) — the sequence that satisfies the statements conditions.

If there are multiple answers, print any of them.

Examples

input

Copy

4
3 3 2
1 2 0

output

Copy

YES
1 3 2 0 

input

Copy

3
1 3
3 2

output

Copy

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=a1t1|t2=(012)|(112)=(112)=3=a1 and t1&t2=(012)&(112)=(012)=1=b1t1&t2=(012)&(112)=(012)=1=b1;
  • t2|t3=(112)|(102)=(112)=3=a2t2|t3=(112)|(102)=(112)=3=a2 and t2&t3=(112)&(102)=(102)=2=b2t2&t3=(112)&(102)=(102)=2=b2;
  • t3|t4=(102)|(002)=(102)=2=a3t3|t4=(102)|(002)=(102)=2=a3 and t3&t4=(102)&(002)=(002)=0=b3t3&t4=(102)&(002)=(002)=0=b3.

In the second example there is no such sequence.

题意:输入一个n,表示需要求的序列的长度,然后给出长度n-1的a序列每个值,b序列每个值都是0到3的,问是否有一个长度为n的序列,相邻两项的或(|)是依次是a的值,与(&)依次是b的值。

题解:当时没做出来,wa 7下来看代码,大体是思路最开始第一项四个方向(0,1,2,3)依次接着推下一个的值或者搜索,如果有满足a,b序列的就记录下来,尝试当前的4个值,依次与前面的值进行|和&,满足保存,不满足就输出No。

c++:

#include <bits/stdc++.h>
using namespace std;
int andd[4][4];
int orr[4][4];
int n;
const int maxn=1e5+7;
int a[maxn]; int b[maxn];
int t[maxn];
void solve(int i){
	if (i==n+1) {
		cout<<"YES"<<endl;
		for (int j=1; j<=n; j++){
			cout<<t[j]<<" ";

		}
		exit(0);
	}

	for (int j=0; j<=3; j++){
		if (((t[i-1]&j)==b[i-1]) &&(( t[i-1]|j)==a[i-1])) t[i]=j,solve(i+1);
	}
}
int main(){
	cin>>n;
	for (int i=1; i<n; i++) cin>>a[i];
	for (int j=1; j<n; j++) cin>>b[j];
	for (int i=0; i<=3; i++) t[1]=i,solve(2);

	cout<<"NO"<<endl;
	return 0;
}

python:

n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
for i in range(4):
    ans=[-1]*n
    ans[0]=i
    flag=False
    for j in range(1,n):
        flag=False
        for k in range(4):
            if a[j-1]==(k|ans[j-1]) and b[j-1]==(k&ans[j-1]):
                ans[j]=k
                flag=True
        if not flag:
            break
    if flag:
        print("YES")
        print(*ans)
        exit()
print("NO")

猜你喜欢

转载自blog.csdn.net/memory_qianxiao/article/details/83269074