zoukankan      html  css  js  c++  java
  • 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
    ");
    22         for (int i=1;i<n;i++){
    23             printf("%d ",t[i]);
    24         }
    25         printf("%d
    ",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
    ");
    50     return 0;
    51 }
  • 相关阅读:
    红黑树
    jsp简单练习-简单的下拉表单
    【源代码】TreeMap源代码剖析
    ScrollView垂直滚动控件
    进度条控件基本使用
    时间对话框的使用
    DatePicker日期与时间控件
    ImageView显示网络上的图片
    bitmap==null
    android.os.NetworkOnMainThreadException
  • 原文地址:https://www.cnblogs.com/-Ackerman/p/11371463.html
Copyright © 2011-2022 走看看