B. Curiosity Has No Limits

题目链接:http://codeforces.com/problemset/problem/1072/B

 

题意:

       给出长度为n-1的两个数组a和b,要求找出一个长度为n的数组t,使得t[i]|t[i+1]=a[i] && t[i]&t[i+1]=b[i],问是否存在这样的数组t

  第一行输入一个n ( 2 < = n < = 1e5) 表示t数组的长度

  第二行输入n-1个数 a1,a2……an-1 (0 < = ai < =3)

  第三行输入n-1个数b1,b2……bn-1 (0 < = bi < =3)

 

思路:

这居然是个 dfs 的题目。因为 a[i] 和 b[i] 的范围在[0,3] ,直接枚举 t 的值就可以过了?!

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <stdlib.h>
 4 #include <cstring>
 5 #include <string>
 6 #include <string.h>
 7 #include <set>
 8 #include <queue>
 9 #include <stdbool.h>
10 
11 #define LL long long
12 using namespace std;
13 const int maxn = 1e5 + 10;
14 
15 
16 int a[maxn],b[maxn],t[maxn];
17 int n;
18 
19 void DFS(int x){
20     if (x<1){
21         printf("YES\n");
22         for (int i=1;i<n;i++){
23             printf("%d ",t[i]);
24         }
25         printf("%d\n",t[n]);
26         exit(0);
27     }
28     for (int i=0;i<=3;i++){
29         if (((i|t[x+1]) == a[x]) && ((i&t[x+1]) == b[x])){
30             t[x] = i;
31             DFS(x-1);
32         }
33     }
34 }
35 
36 
37 int main(){
38     scanf("%d",&n);
39     for (int i=1;i<n;i++){
40         scanf("%d",&a[i]);
41     }
42     for (int i=1;i<n;i++){
43         scanf("%d",&b[i]);
44     }
45     for (int i=0;i<=3;i++){
46         t[n] = i;
47         DFS(n-1);
48     }
49     printf("NO\n");
50     return 0;
51 }

 

posted @ 2019-08-18 09:35  _Ackerman  阅读(236)  评论(0编辑  收藏  举报