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. [Xcode 实际操作]七、文件与数据-(15)单例模式的使用

    目录:[Swift]Xcode实际操作 本文将演示单例对象的使用. 在项目名称上点击鼠标右键,弹出右键菜单,选择[New File]新建文件命令, 在弹出的模板选项窗口中,选择[Swift]文件选项, ...

  2. Promise对象深入理解

     目录 基本用法 返回另一个 Promise 实例 Promise.prototypeof.then Promise.prototype.catch Promise.prototype.finally ...

  3. 面向对象-mixin设计模式的应用(多继承应用场景)

    什么是设计模式? 设计模式只是一种开发思想.不是什么固定的格式. 前人的好的思想,我们后人拿过来用! mixin设计模式: 1.mixin设计迷失可以在不对类的内容的修改前提下,扩展类的功能(添加父类 ...

  4. E. Cyclic Components (DFS)(Codeforces Round #479 (Div. 3))

    #include <bits/stdc++.h> using namespace std; *1e5+; vector<int>p[maxn]; vector<int&g ...

  5. android okhttp和webview session共享

    public static OkHttpClient get(Context context){ OkHttpClient.Builder builder = new OkHttpClient.Bui ...

  6. Codeforces Round #396 (Div. 2) B

    Mahmoud has n line segments, the i-th of them has length ai. Ehab challenged him to use exactly 3 li ...

  7. ORA-00972_标识符过长

    执行SQL查询报:"ORA-00972:标识符过长"错误. 执行SQL: SELECT T.F_FTBS, T.F_TZMC "X组/XXXX/XXXX名称", ...

  8. Springboot日志配置探索(主要看logback)(二)

    这篇博客主要是讲在Springboot中扩展的日志框架的配置,也是主要讲logback 8 继续看文档,这里讲到: springboot里面还有几个日志系统框架可以选择使用,你可以通过在classpa ...

  9. attribute与property区别总结

    在前阵子看JQuery源码中,attr()的简单理解是调用了element.getAttribute()和element.setAttribute()方法,removeAttr()简单而言是调用ele ...

  10. 51nod 1640 天气晴朗的魔法 二分 + 克鲁斯卡算法(kruskal算法) 做复杂了

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1640 一开始想的时候,看到要使得最大值最小,那这样肯定是二分这个最大值了 ...