CF-1027-B. Curiosity Has No Limits
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的更多相关文章
- 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 ...
- CF 1027 F. Session in BSU
F. Session in BSU https://codeforces.com/contest/1027/problem/F 题意: n场考试,每场可以安排在第ai天或者第bi天,问n场考完最少需要 ...
- cf1072B. Curiosity Has No Limits(枚举)
题意 题目链接 给出两个序列\(a, b\),求出一个序列\(t\),满足 \[a_i = t_i | t_{i + 1}\] \[b_i = t_i \& t_{i + 1}\] 同时,\( ...
- CF1072B Curiosity Has No Limits
思路: 对于序列t,只要第一个数确定了,后续的数也随之确定了.枚举四种情况即可.实现: #include <iostream> #include <vector> using ...
- Technocup 2019 - Elimination Round 2
http://codeforces.com/contest/1031 (如果感觉一道题对于自己是有难度的,不要后退,懂0%的时候敲一遍,边敲边想,懂30%的时候敲一遍,边敲边想,懂60%的时候敲一遍, ...
- Codeforces 1072 - A/B/C/D - (Done)
链接:http://codeforces.com/contest/1072/ A - Golden Plate - [计算题] #include<bits/stdc++.h> using ...
- 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++ ...
- java常用的格式化
日常工作中,总会遇到一些格式化显示的需求,下面做一些简单的整理 JDK中java.text下提供了格式化常用的工具类,具体结构见下图 时间日期格式化 DateFormat 采用DateFormat.g ...
- 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 ...
随机推荐
- Mybatis中分页存在的坑1
站在巨人的肩膀上 https://www.cnblogs.com/esileme/p/7565184.html 环境:Spring 4.2.1 Mybatis 3.2.8 pagehelper 5.1 ...
- js动态实现文本框不可编辑状态
两种方法: $("#id").attr("readOnly",false); 不可编辑,可以传值 $("#id").attr("d ...
- FMDB存储模型对象(以二进制存储)用NSKeyedArchiver archivedDataWithRootObject序列号,NSKeyedUnarchiver unarchiveObjectWithData反序列化(重点坑是sql语句@"insert into t_newsWithChannel (nwesName,newsType) values (?,?)")一定要用占位符
交友:微信号 dwjluck2013 一.封装FMDB单例 (1)JLFMDBHelp.h文件 #import <Foundation/Foundation.h> #import < ...
- 应用性能监控-web系统
1 系统规划 参考https://mp.weixin.qq.com/s/UlnHOaN0xaA0jfg5CEmLRA 1.1 数据采集的原则: 数据采集,说起来比较简单,只要把数据报上来就行,具体怎么 ...
- [软件工程基础]2017.11.01 第五次 Scrum 会议
具体事项 燃尽图 每人工作内容 成员 已完成的工作 计划完成的工作 工作中遇到的困难 游心 #8 掌握 Laravel 框架 #10 搭建可用的开发测试环境:#9 阅读分析 PhyLab 后端代码与文 ...
- [coci2015-2016 coii] dijamant【图论】
传送门:http://www.hsin.hr/coci/archive/2015_2016/ 进去之后的最下面的国家赛.顺便说一句,dijamant是克罗地亚语的“钻石”的意思. 官方题解是说压位的暴 ...
- UVa-11582:Colossal Fibonacci Numbers!(模算术)
这是个开心的题目,因为既可以自己翻译,代码又好写ヾ(๑╹◡╹)ノ" The i’th Fibonacci number f(i) is recursively defined in the f ...
- [洛谷P1434] [SHOI2007]滑雪
题目链接: here we go 题外话: 谁能想到这是一道咕了两年的\(AC\)呢--当年是在搜索还半懂不懂的时候遇到的这道题,感觉真是难得要命()所以一直拖着不做,后面就下意识地逃避了搜索相关的内 ...
- STM32F4之SWO
https://stm32f4-discovery.net/2014/12/library-46-debug-stm32f4-device-swo-feature/
- 最大流bfs
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...