Codeforces 1079C Playing Piano(记忆化搜索)
题目链接:Playing Piano
题意:给定长度为n的序列$a_i$,要求得到同样长度的序列$b_i$。满足一下条件:
if $a_i < a_{i+1}$ then $b_i < b_{i+1}$
if $a_i > a_{i+1}$ then $b_i > b_{i+1}$
if $a_i = a_{i+1}$ then $b_i \neq\ b_{i+1}$
$1 <= b_i <= 5$
题解:第一个数从1-5枚举,接着从下一位开始搜索,dp[i][j]表示第i位数字为j的情况是否已经搜索过。
当$a_i<a_{i+1}$的时候,从当前值往后搜索;当$a_i>a_{i+1}$的时候,从当前值往前搜索;$a_i=a_{i+1}$的时候,搜索和当前值不相同的值。搜索的区间范围为1-5。
#include <cstdio>
#include <cstdlib>
using namespace std; const int N=1e5+;
int n,a[N],b[N],dp[N][]; void dfs(int x,int val){
if(x>n){
printf("%d",b[]);
for(int i=;i<=n;i++) printf(" %d",b[i]);
printf("\n");
exit();
}
if(dp[x][val]) return ;
dp[x][val]=;
if(a[x]>a[x-]){
for(int i=val+;i<=;i++){
b[x]=i;
dfs(x+,i);
}
}
else if(a[x]<a[x-]){
for(int i=;i<=val-;i++){
b[x]=i;
dfs(x+,i);
}
}
else{
for(int i=;i<=;i++){
if(i!=val){
b[x]=i;
dfs(x+,i);
}
}
}
} int main(){
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
for(int i=;i<=;i++){
b[]=i;
dfs(,i);
}
printf("-1\n");
return ;
}
Codeforces 1079C Playing Piano(记忆化搜索)的更多相关文章
- CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化
Spiral Maximum 题目连接: http://codeforces.com/problemset/problem/173/C Description Let's consider a k × ...
- CodeForces 398B 概率DP 记忆化搜索
题目:http://codeforces.com/contest/398/problem/B 有点似曾相识的感觉,记忆中上次那个跟这个相似的 我是用了 暴力搜索过掉的,今天这个肯定不行了,dp方程想了 ...
- CodeForces 918D MADMAX(博弈+记忆化搜索)
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- CodeForces 132C Logo Turtle (记忆化搜索)
Description A lot of people associate Logo programming language with turtle graphics. In this case t ...
- Codeforces 667C Reberland Linguistics 记忆化搜索
链接 Codeforces 667C Reberland Linguistics 题意 给你一个字符串,除去前5个字符串后,使剩下的串有长度为2或3的词根组成,相邻的词根不能重复.找到所有的词根 思路 ...
- Codeforces #564div2 E1(记忆化搜索)
虽然不是正解但毕竟自己做出来了还是记下来吧- 对每个人分别dfs得到其期望,某两维的组合情况有限所以Hash一下避免了MLE. #include <cstdio> #include < ...
- Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索
A. Berzerk 题目连接: http://codeforces.com/contest/786/problem/A Description Rick and Morty are playing ...
- Codeforces Gym 100231G Voracious Steve 记忆化搜索
Voracious Steve 题目连接: http://codeforces.com/gym/100231/attachments Description 有两个人在玩一个游戏 有一个盆子里面有n个 ...
- Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索
D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...
随机推荐
- JAVA 递归实现从n个数中选取m个数的所有组合
这周Java课程有个小作业:Java递归实现从n个数中选取m个数的所有组合 代码如下: //其中 n 取 1,2,3,4,5 五个数, m 取 3 package javaText; public c ...
- 【转载】DSP基础--定点小数运算
在FPGA实现算法过程中,大多数情况是用占用资源较少,延迟较低的定点数代替浮点数参与运算.那么浮点与定点数之间的区别以及转换方式是怎么的?下边这篇博文详细说明了这一问题.虽然是针对DSP芯片的,但思想 ...
- js 学习之路8:for循环
1. for循环 <!DOCTYPE html> <html> <meta http-equiv="Content-Type" content=&qu ...
- 虚拟机 与 host主机,无法ping通的问题
这个写的比较简单,先做以下记录 centos虚拟机安装到别的电脑上,因为linux中的程序需要向外有网络互通,所以需要重新设置ip 通过 ifconfig eth4 192.168.0.20 bro ...
- A Deep Learning-Based System for Vulnerability Detection(一)
接着上一篇,讨论讨论具体步骤实现方法.步骤1-3分别在下面进行阐述,步骤4,6都是标准的,步骤5类似于步骤1-3. 结合这个图进行讨论详细步骤: 步骤1:提取库/API函数调用和程序片段 1.1将库/ ...
- 持续代码质量管理-SonarQube-7.3简单使用
安装了SonarQube以及Sonar Scanner之后,就需要那代码检测了.当然为了方便我们使用已有现成的demo,知道到对应的git地址下载即可. 1. sonar-examples下载 htt ...
- LeetCode算法题-Binary Number with Alternating Bits(Java实现)
这是悦乐书的第292次更新,第310篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第160题(顺位题号是693).给定正整数,检查它是否具有交替位:即它的二进制数的任意两 ...
- Git的可视化工具SourceTree管理代码 SourceTree的使用
出处:https://blog.csdn.net/android_zhengyongbo/article/details/72885860 其他参考资料https://www.cnblogs.com/ ...
- Configuring Apache Kafka for Performance and Resource Management
Apache Kafka is optimized for small messages. According to benchmarks, the best performance occurs w ...
- mybatis 错误
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.TooManyR ...