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. 【IDEA】关于 IDEA 中新建 web 项目的 webapp 文件夹没有小蓝点 ,启动服务,访问不到解决方案

    问题描述: 新建的 maven 的 Module 项目,webapp 文件夹也是在创建完项目后手动添加的,出现了 webapp 文件夹不能被识别的情况. 解决方案: 第一步:  选中项目按 F4 键, ...

  2. 一个Java语言所写的shop网站框架明细

    核心框架Spring Framework :作为一个优秀的开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为 J2EE 应用 ...

  3. mysql整理(个人)

    注意:以下命令都是在Linux系统下执行的: 1.验证mysql是否安装成功: mysqladmin --version 2.连接mysql服务器: mysql -u root -p 之后输入密码 3 ...

  4. Codeforces 526G Spiders Evil Plan

    由于做的时候看的是中文题面,第一遍写就被卡题意了:还以为每一条都要过x,那么就是一道动态树根选择2y个叶子的奇怪题目 交完0分gg,才发现题目看错了╮(╯▽╰)╭ the node containin ...

  5. AtCoder Beginner Contest 051 ABCD题

    A - Haiku Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement As a New Yea ...

  6. 洛谷P3177||bzoj4033 [HAOI2015]树上染色

    洛谷P3177 bzoj4033 根本不会做... 上网查了题解,发现只要在状态定义的时候就考虑每一条边全局的贡献就好了? 考虑边的贡献和修改状态定义我都想到了,然而并不能想到要结合起来 ans[i] ...

  7. odoo9 部署步详细步骤

    sudo apt-get updatesudo apt-get dist-upgrade 一:安装和配置pg sudo apt-get install  postgresql sudo su - po ...

  8. Exception in thread "main" java.lang.SecurityException: class "javax.servlet.FilterRegistration"'s signer information does not match signer information of other classes in the same package解决办法(图文详解)

    不多说,直接上干货! 问题详情 SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF ...

  9. CSS浮动float父div没有高度的问题

    如下所示,子元素 div2 本身具有高度和宽度,但由于其具有float:left:属性后.其父元素 div1 不具有高度. <html>    <head>    </h ...

  10. 《JavaScript设计模式》笔记之第三章:封装和信息隐藏

    第三章 创建对象的基本模式 方法一:门户大开型 var Book = function(isbn, title, author) {   if(isbn == undefined ) throw ne ...