题目链接: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(记忆化搜索)的更多相关文章

  1. CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化

    Spiral Maximum 题目连接: http://codeforces.com/problemset/problem/173/C Description Let's consider a k × ...

  2. CodeForces 398B 概率DP 记忆化搜索

    题目:http://codeforces.com/contest/398/problem/B 有点似曾相识的感觉,记忆中上次那个跟这个相似的 我是用了 暴力搜索过掉的,今天这个肯定不行了,dp方程想了 ...

  3. CodeForces 918D MADMAX(博弈+记忆化搜索)

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  4. CodeForces 132C Logo Turtle (记忆化搜索)

    Description A lot of people associate Logo programming language with turtle graphics. In this case t ...

  5. Codeforces 667C Reberland Linguistics 记忆化搜索

    链接 Codeforces 667C Reberland Linguistics 题意 给你一个字符串,除去前5个字符串后,使剩下的串有长度为2或3的词根组成,相邻的词根不能重复.找到所有的词根 思路 ...

  6. Codeforces #564div2 E1(记忆化搜索)

    虽然不是正解但毕竟自己做出来了还是记下来吧- 对每个人分别dfs得到其期望,某两维的组合情况有限所以Hash一下避免了MLE. #include <cstdio> #include < ...

  7. Codeforces Round #406 (Div. 1) A. Berzerk 记忆化搜索

    A. Berzerk 题目连接: http://codeforces.com/contest/786/problem/A Description Rick and Morty are playing ...

  8. Codeforces Gym 100231G Voracious Steve 记忆化搜索

    Voracious Steve 题目连接: http://codeforces.com/gym/100231/attachments Description 有两个人在玩一个游戏 有一个盆子里面有n个 ...

  9. Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索

    D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...

随机推荐

  1. C#中的yield return用法演示源码

    下边代码段是关于C#中的yield return用法演示的代码. using System;using System.Collections;using System.Collections.Gene ...

  2. delphi做的程序如何连接SQL数据库

    1. 新建一个FORM.在控件栏找到ADO一栏,把里面的ADOConnection和ADOQuery两个各建立一个放在FORM里.这两个控件运行后是不可见的,所以你可以随便放在FORM的任何位置.然后 ...

  3. t-sql语句创建表(基础)

    create table ta1 (     id int identity(1,2) not null,     name nvarchar(20) not null,     identify v ...

  4. 使用Linq的泛型功能

    泛型数据访问类: 业务抽象类使用数据访问类: 业务类继承业务抽象类: 使用业务类:

  5. SQL语句中不同的连接JOIN

    为了从两个表中获取数据,我们有时会用JOIN将两个表连接起来.通常有以下几种连接方式: JOIN  or  INNER JOIN(内连接) : 这两个是相同的,要求两边表同时有对应的数据,返回行,任何 ...

  6. GCD多线程的一个全面的题目

    GCD多线程的一个全面的题目  

  7. linux 磁盘IO测试工具:FIO (同时简要介绍dd工具测试)

    FIO是测试IOPS的非常好的工具,用来对硬件进行压力测试和验证.磁盘IO是检查磁盘性能的重要指标,可以按照负载情况分成照顺序读写,随机读写两大类. 目前主流的第三方IO测试工具有fio.iomete ...

  8. mysql 的链接字符

    mysql的链接字符: driver =com.mysql.cj.jdbc.Driverurl =jdbc:mysql://localhost:3306/oa?serverTimezone=Asia/ ...

  9. Linux Collection:网络配置

    PAS 缺少ifconfig 安装相应软件[不推荐],尽量使用 ip 命令 sudo apt install gnome-nettool 补充,显示IP地址: ip show address PAS ...

  10. PostgreSQL:Java使用CopyManager实现客户端文件COPY导入

    在MySQL中,可以使用LOAD DATA INFILE和LOAD DATA LOCAL INFILE两种方式导入文本文件中的数据到数据库表中,速度非常快.其中LOAD DATA INFILE使用的文 ...