http://codeforces.com/contest/1072/problem/B

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长度的t数组,满足t[i]&t[i+1] == b[i]   同时 t[i]|t[i+1] == a[i]。 a,b数组长度为n-1

枚举法,想过枚举但是没有想到是这样做的。因为t数组中,最后一个是最特殊的,只与a和b数组中的一个元素相关(自己没有发现)。同时没有发现如果确定了一个点,那么下一个点的值是唯一确定的。这样想来,其实枚举第一个也是可行的了。

同时,需要记住,如果t[i]确定了,那么与不同的t[i+1]进行或,与运算一定会得到的是不同的结果。

和之前做过的一道Fliptile有些像。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
#define ll long long
//#define local using namespace std; const int MOD = 1e9+;
const int inf = 0x3f3f3f3f;
const double PI = acos(-1.0);
const int maxn = (1e5+);
const int maxedge = *;
int a[maxn], b[maxn];
int t[maxn]; int main() {
#ifdef local
if(freopen("/Users/Andrew/Desktop/data.txt", "r", stdin) == NULL) printf("can't open this file!\n");
#endif
int n;
scanf("%d", &n);
for (int i = ; i < n-; ++i) {
scanf("%d", a+i);
}
for (int i = ;i < n-; ++i) {
scanf("%d", b+i);
}
for (int i = ; i < ; ++i) {
memset(t, -, sizeof(t));
t[n-] = i;
for (int j = n-; j >= ; --j) {
for (int k = ; k < ; ++k) {
if ((k|t[j+])==a[j] && (k&t[j+])==b[j]) {
t[j] = k;
break;
}
}
if (t[j] == -) break;
}
if (t[] != -) break;
}
if (t[] == -) printf("NO\n");
else {
printf("YES\n");
for (int i = ; i < n; ++i) {
printf("%d", t[i]);
if (i != n-) printf(" ");
}
printf("\n");
}
#ifdef local
fclose(stdin);
#endif
return ;
}
 

CodeForce 517 Div 2. B Curiosity Has No Limits的更多相关文章

  1. CodeForce 517 Div 2. C Cram Time

    http://codeforces.com/contest/1072/problem/C C. Cram Time time limit per test 1 second memory limit ...

  2. Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2)

    Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2) #include <bits/stdc++ ...

  3. CF-1027-B. Curiosity Has No Limits

    CF-1027-B. Curiosity Has No Limits http://codeforces.com/contest/1072/problem/B 题意: 给定两组序列a,b,长度为n-1 ...

  4. Codeforce#331 (Div. 2) A. Wilbur and Swimming Pool(谨以此题来纪念我的愚蠢)

    C time limit per test 1 second memory limit per test 256 megabytes input standard input output stand ...

  5. 【LCA】CodeForce #326 Div.2 E:Duff in the Army

    C. Duff in the Army Recently Duff has been a soldier in the army. Malek is her commander. Their coun ...

  6. CODEFORCE 246 Div.2 B题

    题目例如以下: B. Football Kit time limit per test 1 second memory limit per test 256 megabytes input stand ...

  7. Codeforces Round #517 Div. 2/Div. 1

    \(n\)天没更博了,因为被膜你赛的毒瘤题虐哭了... 既然打了这次CF还是纪念一下. 看看NOIP之前,接下来几场的时间都不好.这应该是最后一场CF了,差\(4\)分上紫也是一个遗憾吧. A 给一个 ...

  8. Codeforces Round #517 (Div. 2)

    A #include<queue> #include<cstdio> #include<cstring> #include<algorithm> #de ...

  9. Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2) D. Minimum path

    http://codeforces.com/contest/1072/problem/D bfs 走1步的最佳状态 -> 走2步的最佳状态 -> …… #include <bits/ ...

随机推荐

  1. 【Entity Framework】disable automatic migration, 执行update-migration仍然会显示有automatic migration

    本文涉及的相关问题,如果你的问题或需求有与下面所述相似之处,请阅读本文 [Entity Framework] disable automatic migration, 执行update-migrati ...

  2. Perl新接触的小命令

    0,glob()函数 my @infile = glob("$indir/*.txt.gz");  #获得指定路径下的文件名,并形成列表,如果$indir为空,则自动匹配当前目录下 ...

  3. pixi.js(入门)

    1.关于 一个关于HTML5 2D渲染引擎,它的独特之处在于其拥有了canvas回调功能的WebGL,速度快,能够兼容所有设备,简单得说也就是跨平台了,我用的开发工具是WebStorm 2.参考API ...

  4. 【读书笔记】Cronjob原理及源码分析

    原文链接:https://mp.weixin.qq.com/s?__biz=MzI0NjI4MDg5MQ==&mid=2715291842&idx=1&sn=e605f9b40 ...

  5. linux存储管理之自动挂在

    自动挂载 Automount ==================================================================================== ...

  6. 手机端页面可以左右轻微拖动的bug

    在做项目的时候,必须要适应各种屏幕,移动端是必须的. 但是在移动端中,网站是左右可以动,怎么办呢? 这是只要在样式表中写入 html,body{overfow-x:hidden;} bug产生在H5的 ...

  7. 【百度地图API】如何获取行政区域的边界?(转载)

    转自:http://www.cnblogs.com/milkmap/archive/2012/04/11/2442430.html 摘要:以前教过大家如何自行获取行政区域,或者自定义获取一个区域的边界 ...

  8. noip模拟ernd

    [题目背景]解决了第一题之后,你打开了第二题.这是一道关于树的题目,你很快想出了一个复杂度和树的直径有关的算法,可以通过所有的数据.不过,你的做法常数似乎有点大.为了更好地卡常,你决定构造一些数据来检 ...

  9. 精选!15 个必备的 VSCode 插件(前端类)

      精选!15 个必备的 VSCode 插件(前端类)   就像大多数 IDE 一样,VSCode 也有一个扩展和主题市场,包含了数以千计质量不同的插件.为了帮助大家挑选出值得下载的插件,我们针对性的 ...

  10. Qt 布局管理

    在布局编辑环境里: sizePolicy 影响控件在布局上的大小. layout 的属性 如:如margin 设置控件在布局上边距. 有时候需要设置 下面是一个 代码布局的方式 #include &q ...