Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round B. Curiosity Has No Limits

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011469138/article/details/83722328

B. Curiosity Has No Limits

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.
搜索题,dfs就过了

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int n,a[100005],b[100005],ans[100005],flag=0;
void dfs(int pos,int pre)
{
	
	if(pos>n-1&&!flag)
		{
			flag=1;
			printf("YES\n");
			for(int i=0;i<=n-1;i++)
				printf("%d ",ans[i]);
			cout<<endl;
		}
	else
		for(int i=0;i<=3;i++)
		{
			
			if(((pre|i)==a[pos])&&((pre&i)==b[pos])||(pos==n-1))
			{
				
				//printf("%d",i);
				ans[pos]=pre;
				dfs(pos+1,i);	
			}
		}
}

int main()
{
	//printf("%d %d\n",3|2,3&2);
	scanf("%d",&n);
	for(int i=0;i<n-1;i++)
		scanf("%d",&a[i]);
	for(int i=0;i<n-1;i++)
		scanf("%d",&b[i]);
	for(int i=0;i<=3;i++)
	{	
		
		dfs(0,i);
		if(flag)	
			break;
		
	}
	if(flag==0)
		printf("NO\n");
		
	return 0;
}

猜你喜欢

转载自blog.csdn.net/u011469138/article/details/83722328