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. IT兄弟连 JavaWeb教程 Servlet会话跟踪 获取Session对象

    Session对象的获取有两种: ●  有参方法: HttpSession request.getSession(boolean isNew) 参数: true:获取一个Session对象,如果之前S ...

  2. git 创建分支并关联远程分支

    从master分支,重新拉取出一个新的分支,名字为dev,具体命令如下: 1. 切换到被copy的分支(master),从服务器拉取最新版本 $git checkout master $git pul ...

  3. SpringBoot | Hibernate @Transient 注解

    在默认情况下,持久化类的所有属性会自动映射到数据表的数据列.如果在实际应用中,不想持久保存某些属性,则可以考虑使用@Transient来修饰它们. 如果一个属性并非数据库表的字段映射,就务必将其标示为 ...

  4. JSP | 基础 | Button跳转页面

    <input type = "button" value = "登陆" onclick = "window.location.href = 'L ...

  5. [題解]51nod_1515_明辨是非

    好久沒有話多了,是覺得有點浪費時間,今天考試和一中用的一樣的題,結果反而考得不好,不過Jackpei一句知恥而後勇點醒夢中人偷偷@Jackpei 就是這樣吧 還有我極度懷疑我的鍵帽打油了......我 ...

  6. Spark Mllib里如何将数据集按比例随机地分成trainData、testData和validationData数据集(图文详解)

    不多说,直接上干货! 具体详情见 Hadoop+Spark大数据巨量分析与机器学习整合开发实战的第11章 电影推荐引擎

  7. hdu3487Play with Chain(splay)

    链接 简单的两种操作,一种删除某段区间,加在第I个点的后面,另一个是翻转区间.都是splay的简单操作. 悲剧一:pushdown时候忘记让lz=0 悲剧二:删除区间,加在某点之后的时候忘记修改其父亲 ...

  8. split()分割字符串用法

    <script type="text/javascript"> var str="How are you doing today?" documen ...

  9. React 实践记录 01 组件开发入门

    Introduction 本文组成: Ryan Clark文章Getting started with React的翻译. 博主的实践心得. React由Facebook的程序员创建,是一个非常强大的 ...

  10. Webservice相关的知识

    一.利用jdk web服务api实现,这里使用基于 SOAP message 的 Web 服务 1.首先建立一个Web services EndPoint: package Hello; import ...