博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CodeForce 517 Div 2. B Curiosity Has No Limits
阅读量:4318 次
发布时间:2019-06-06

本文共 3713 字,大约阅读时间需要 12 分钟。

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 n1n−1 on the blackboard. Let's denote the elements of the first sequence as aiai (0ai30≤ai≤3), and the elements of the second sequence as bibi (0bi30≤bi≤3).

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

  • ai=ti|ti+1ai=ti|ti+1 (where || denotes the ) and 
  • bi=ti&ti+1bi=ti&ti+1 (where && denotes the ). 

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 (2n1052≤n≤105) — the length of the sequence titi. 

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

The third line contains n1n−1 integers b1,b2,,bn1b1,b2,…,bn−1 (0bi30≤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 (0ti30≤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 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long//#define localusing namespace std;const int MOD = 1e9+7;const int inf = 0x3f3f3f3f;const double PI = acos(-1.0);const int maxn = (1e5+10);const int maxedge = 100*100;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 = 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 < 4; ++i) { memset(t, -1, sizeof(t)); t[n-1] = i; for (int j = n-2; j >= 0; --j) { for (int k = 0; k < 4; ++k) { if ((k|t[j+1])==a[j] && (k&t[j+1])==b[j]) { t[j] = k; break; } } if (t[j] == -1) break; } if (t[0] != -1) break; } if (t[0] == -1) printf("NO\n"); else { printf("YES\n"); for (int i = 0; i < n; ++i) { printf("%d", t[i]); if (i != n-1) printf(" "); } printf("\n"); }#ifdef local fclose(stdin);#endif return 0;}
View Code

 

 

 

 

转载于:https://www.cnblogs.com/lecoz/p/9833963.html

你可能感兴趣的文章
Git(四) - 分支管理
查看>>
PHP Curl发送数据
查看>>
HTTP协议
查看>>
CentOS7 重置root密码
查看>>
Centos安装Python3
查看>>
PHP批量插入
查看>>
laravel连接sql server 2008
查看>>
Laravel框架学习笔记之任务调度(定时任务)
查看>>
Swagger在Laravel项目中的使用
查看>>
Laravel 的生命周期
查看>>
Nginx
查看>>
Navicat远程连接云主机数据库
查看>>
Nginx配置文件nginx.conf中文详解(总结)
查看>>
influxdb 命令行输出时间为 yyyy-MM-dd HH:mm:ss(年月日时分秒)的方法
查看>>
jxl写入excel实现数据导出功能
查看>>
linux文件目录类命令|--cp指令
查看>>
.net MVC 404错误解决方法
查看>>
linux系统目录结构
查看>>
git
查看>>
btn按钮之间事件相互调用
查看>>