CF-1027-B. Curiosity Has No Limits

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

题意:

给定两组序列a,b,长度为n-1。求数列t使得

a[i] = t[i]|t[i+1]

b[i] = t[i]&t[i+1]

其中( \(0\le a[i]\le3\) , \(0 \le b[i] \le 3\) )

分析:

  • 刚看到这个题,感觉是dp,然后觉得范围只有0~3,可以分情况讨论,奈何写不出来转移方程于是dfs。然而写dfs也只是抓住每个情况不放,导致代码极丑无比。

DP

  • t[i+1]t[i] a[i+1] b[i+1]共同决定,而a[i+1],b[i+1]i+1表示,只需要记录t[i]即可。
  • d[i+1][j]表示在 i+1阶段,t[i+1]j时,t[i]应该为多少。
  • 状态转移方程:d[i+1][l] = j( (l|j) == a[i] && (l&j) == b[i] )
  • 由于t[i]范围是0~3,所以先把d数组初始化为-1,表示都不能储存。并且由上述状态转移方程可以看出,我们把记忆化搜索路径已经存放到了d数组里面,最后倒序遍历存放到vecotr之后即可正序输出。
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 1;
int a[N], b[N], dp[N][4],t[N];
int main() {
//加快cin输入,cout输出
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n;
cin >> n;
for (int i = 1; i < n; ++i)
cin >> a[i];
for (int j = 1; j < n; ++j)
cin >> b[j];
memset(dp,-1,sizeof dp);
for (int i = 0; i < 4; ++i)
dp[1][i] = 0;
/***核心***/
for (int i = 1; i < n; ++i)
for (int j = 0; j <= 3; ++j)
if (dp[i][j] >= 0)
for (int l = 0; l <= 3; ++l)
if (a[i] == (j | l) && b[i] == (j & l))
dp[i + 1][l] = j;
/***找到任意一组答案直接倒序遍历然后输出即可***/
for (int i = 0; i < 4; ++i)
{
if (dp[n][i] >= 0)
{
cout << "YES" << endl;
vector <int> ans;
int p = i, j = n;
for (j = n; j > 0; --j)
{
ans.push_back(p);
p = dp[j][p];
}
for (j = ans.size() - 1; j >= 0; --j)
cout << ans[j] << ' ';
return 0;
}
}
cout << "NO";
}

DFS

  • 每一层搜索,都要有上一层的t[i]来作为依据,不过我们把t[i]放到全局即可,不必放到dfs的参数中。参数只需记录搜索层数即可。
int n,cnt,flag;
void dfs(int p)
{
if(flag)return;
if(p==n-1)
{
flag = 1;
printf("YES\n");
for(int i=0;i<n;i++)
printf("%d ",t[i]);
return;
}
for(int i=0;i<=3;i++)
if((t[p]|i) == a[p]&&(t[p]&i)==b[p])
{
t[++p] = i;
dfs(p);
}
}
int main()
{
while(~scanf("%d",&n))
{
cnt = 0;
flag = 0;
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++)
{
if(flag) break;
t[0] = i;
dfs(0);
}
if(flag == 0)printf("NO\n");
}
}

总结:

  • 此题dfs代码好写,细节不用考虑太多。但效率不如dp。
  • 即便每一层情况很少,也不是一定分组考虑,有时直接遍历会更加方便。大神十分钟ac的题我却足足耗了四十分钟。

CF-1027-B. Curiosity Has No Limits的更多相关文章

  1. CodeForce 517 Div 2. B Curiosity Has No Limits

    http://codeforces.com/contest/1072/problem/B B. Curiosity Has No Limits time limit per test 1 second ...

  2. CF 1027 F. Session in BSU

    F. Session in BSU https://codeforces.com/contest/1027/problem/F 题意: n场考试,每场可以安排在第ai天或者第bi天,问n场考完最少需要 ...

  3. cf1072B. Curiosity Has No Limits(枚举)

    题意 题目链接 给出两个序列\(a, b\),求出一个序列\(t\),满足 \[a_i = t_i | t_{i + 1}\] \[b_i = t_i \& t_{i + 1}\] 同时,\( ...

  4. CF1072B Curiosity Has No Limits

    思路: 对于序列t,只要第一个数确定了,后续的数也随之确定了.枚举四种情况即可.实现: #include <iostream> #include <vector> using ...

  5. Technocup 2019 - Elimination Round 2

    http://codeforces.com/contest/1031 (如果感觉一道题对于自己是有难度的,不要后退,懂0%的时候敲一遍,边敲边想,懂30%的时候敲一遍,边敲边想,懂60%的时候敲一遍, ...

  6. Codeforces 1072 - A/B/C/D - (Done)

    链接:http://codeforces.com/contest/1072/ A - Golden Plate - [计算题] #include<bits/stdc++.h> using ...

  7. 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++ ...

  8. java常用的格式化

    日常工作中,总会遇到一些格式化显示的需求,下面做一些简单的整理 JDK中java.text下提供了格式化常用的工具类,具体结构见下图 时间日期格式化 DateFormat 采用DateFormat.g ...

  9. Centos 7 mysql Buffered warning: Changed limits: max_connections: 214 解决方法

    Everytime I restart MySQL I have this warning: [Warning] Buffered warning: Changed limits: max_conne ...

随机推荐

  1. 新手安装 hadoop、hive和hbase 笔记

    系统是ubuntu 12.04 , hadoop版本是1.2.1 , hive版本是0.12 , hbase版本我忘记了,不好意思首先是配置好hostnamevi /etc/hosts写入你要配置的i ...

  2. maven - 安装目录详解

    从 Apache Maven 官网下载 Maven 的安装包并解压之后,进入安装目录,我们会看到如下内容: 接下来我们分别解读目录的内容及其功能 bin 包含了mvn运行的脚本,在命令行输入任意一条m ...

  3. C 语言实例 - 字符串排序

    C 语言实例 - 字符串排序 C 语言实例 C 语言实例 按字典顺序排序. 实例 #include<stdio.h> #include <string.h> int main( ...

  4. django_view操作数据库

    1 create def add_area(request): area = Area.objects.create(name='commom',description='a commom area' ...

  5. Python标准库time

    原文:http://www.cnblogs.com/qq78292959/archive/2013/03/22/2975786.html Python官方文档 在程序中,免不了和时间打交道,要学习ti ...

  6. 洛谷1052(路径压缩后简单dp)

    同POJ3744写法都是一样的. 距离太长无意义可以压缩,注意不是随便压的,想一想可以跟%T发生关系. #include <cstdio> #include <cctype> ...

  7. [已读]ppk谈javascript

    读的第一本javascript方面的书籍,印象也比较深.ppk对浏览器兼容很有研究~~可以看看他的www.quirksmode.org

  8. gdb手册

    摘自:https://github.com/hellogcc/100-gdb-tips/blob/master/src/quit-gdb-silently.md. 我只是摘抄我平时没注意到的,或者我认 ...

  9. arcengine geometry union操作

    以前得到的结果老是某一个,用下面的方法就可以获取合并后的结果 IGeometry pUnionGeo = null; var bFirst = true; foreach (IGeometry pGe ...

  10. Mongodb JAVA API

    连接mongodb 1.连接一个mongodb ); 2.连接mongodb集群 MongoClient mongoClient = ), new ServerAddress("localh ...