一、题目描述

A carpenter has received an order for a wooden directional sign. Each board must be aligned vertically with the previous one, either to the basis of the previous arrowhead or to the opposite side, being fixed there with a specially designed screw. The two boards must overlap. The carpenter wrote down a sequence of integers to encode the sketch sent by the designer but the sequence does not determine a unique model and he has thrown away the original sketch. What looked like a trivial task turned out a big jigsaw to him.

The sequence (with 1 + N elements) encodes the (N) arrows from the bottom to the top of the sign. The first element is the position of the left side of the bottom arrow. The remaining N elements define the positions where the arrowheads start, from bottom to top: the i-th element is the position where the i-th arrowhead basis is. For instance, the two signs depicted (on the left and on the right) could be encoded by 2 6 5 1 4.

Since a board must be aligned vertically with the previous one (either to the basis of the previous arrowhead or to the opposite side), if the sequence was 2 6 5 1 4 3, the fifth arrow could be fixed (in any of the depicted signs) with a screw either at 1 (pointing to the right) or at 4 (pointing to the left), with the arrowhead basis at 3.

If the sequence was 2 3 1, the second arrow could only be fixed with a screw at 3, pointing to the left, because consecutive boards must overlap.

All arrowheads are similar and the designer told the carpenter that their bases stand in different vertical lines, as well as the left side of the bottom arrow, altogether forming a permutation of 1..(N +1). That is why the carpenter overlooked the details and just wrote down the permutation (e.g., 2 6 5 1 4 3).

Given the sequence of numbers the carpenter wrote down, compute the number of directional arrows signs that can be crafted. Since the number can be very large, you must write it modulo 2147483647. The second integer in the sequence is always greater than the first one (the bottom arrow points to the right always).

Input

The first line has one integer N and the second line contains a permutation of the integers from 1 to N + 1. Integers in the same line are separated by a single space.

Output

The output has a single line with the number (modulo 2147483647) of distinct signs that can be described by the given permutation.

Note

1 ≤ N ≤ 2

测试输入 期待的输出 时间限制 内存限制 额外进程
测试用例 1 以文本方式显示

  1. 2↵
  2. 2 3 1↵
以文本方式显示

  1. 1↵
1秒 64M 0
测试用例 2 以文本方式显示

  1. 5↵
  2. 2 6 5 1 4 3↵
以文本方式显示

  1. 6↵
1秒 64M 0

二、题目翻译

英文题目啊......翻成中文这事就交给你们手中的翻译软件了,我是说我先凝练一下题目在讲啥,它又要我们干啥。

首先为了方便,我这样描述一个木块:

因为顶部和基只差一个单位长度,所以知道基就知道了顶部。也就是说要描述一个箭头长啥样,我只需要底部和基就行了。

题目向我们描述了这些信息:

①箭头和箭头是一块一块贴着搭上去的。

②从下往上搭的过程中,上面的箭头的底部,必须等于下面的箭头的底部,或者等于下面的箭头的基。

③相邻两个箭头需要有重叠部分,不然钉不住。

我们可以简单知道这些信息:

①相邻的两块箭头,因为上面的箭头的底部有两种可能放法,所以同样的数据自然可能有多种情况。

②题目就是要我们输出有多少种不同的情况。

题目输入共两行,第一行是木板的数量 n ,第二行的数据量为 n+1 ,其中第二行的第一个数据是最下面箭头的底部,之后的n个数据是第n-1行箭头的顶部。

题目要求输出可能的情况数%2147483647。


三、思路

这题难在......题目一开始看不懂。

看懂了以后,很容易想到递推。假设从下往上数第k块箭头的情况数为S(k),第(k+1)块箭头的情况数为S(k+1),S(k+1)和S(k)之间是有关系的。关键在于怎么计算S(k)。


四、代码实现

注意几个点:

①假设 f[i][j] 是从下往上数第 i 块箭头,底部为 j 的情况数,显然S(i) =

②第一块箭头的底部和顶部都已经确定,只有一种情况。

③当 i ≥ 2 时,第 i 块箭头的底部可能有 i 种情况,它们就是Input第二行的前 i 个数。

④鉴于 f[i][j] 可能会非常大(大到超过了2147483647),每一次最好都求一次余。

⑤懒得搞公式了,借用讨论区一张图

不清楚本人是否愿意被知晓名字,先不写。

限于博主的表达能力有限,这些个东西靠你们自己悟吧~


五、完整代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2010;
ll f[N][N];    //f[i][j]表示第i个箭头,当底部为j的时候,可能的情况数。
int nd[N];
//因为end存在关键字重复,这里写的是nd。nd[0]是最下面那一块的底部,之后nd[i]表示第i块的顶部int n;int main(){memset(f,0,sizeof(f));scanf("%d",&n);for (int i=0;i<=n;i++) {scanf("%d",&nd[i]);}f[1][nd[0]] = 1;     //第一块只有一种情况for (int i=2;i<=n;i++) { //第i块for (int p=0;p<=i;p++) {int j = nd[p];if ( (j<nd[i]&&j<nd[i-1]) || (j>nd[i]&&j>nd[i-1]) ) {f[i][j] = f[i-1][j];}else if (j==nd[i-1]) {if (j<nd[i]) {for (int k=j+1;k<=n+1;k++) {f[i][j] += f[i-1][k];}}else {for (int k=1;k<=j-1;k++) {f[i][j] += f[i-1][k];}}}else f[i][j]=0;f[i][j] %= 2147483647;}}ll ans=0;//求第n块木板的情况总数for (int i=1;i<=n+1;i++) {ans += f[n][i];ans %= 2147483647;}cout << ans << endl;return 0;
}

啧啧啧,这篇质量不高,看个乐呵就行。

方向标 | c++ | 动态规划相关推荐

  1. 让我们聊聊聊天机器人

    介绍(Introduction) While surfing a website, you must have come across a popup with an image of a perso ...

  2. 伍六七带你学算法 动态规划 ——不同路径

    力扣 62. 不同路径 难度 中等 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格 ...

  3. 由动态规划计算编辑距离引发的思考

    简单介绍 编辑距离算法: https://www.cnblogs.com/BlackStorm/p/5400809.html https://wizardforcel.gitbooks.io/the- ...

  4. LeetCode 10. Regular Expression Matching python特性、动态规划、递归

    前言 本文主要提供三种不同的解法,分别是利用python的特性.动态规划.递归方法解决这个问题 使用python正则属性 import reclass Solution2:# @return a bo ...

  5. 【动态规划】Part1

    1. 硬币找零 题目描述:假设有几种硬币,如1.3.5,并且数量无限.请找出能够组成某个数目的找零所使用最少的硬币数. 分析:   dp [0] = 0            dp [1] = 1 + ...

  6. 2016.4.2 动态规划练习--讲课整理

    1.codevs1742 爬楼梯  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 小明家外面有一个长长的楼梯,共N阶.小明的腿 ...

  7. 算法设计与分析第4章 动态规划(二)【DP序列问题】

    第3章 动态规划(二)[DP序列问题] 3.2 DP序列问题 (51nod的动态规划教程很不错,讲解很详细,以下分析来自51nod) 1.矩阵取数问题 给定一个m行n列的矩阵,矩阵每个元素是一个正整数 ...

  8. 算法设计与分析第4章 动态规划(一)【背包问题】

    第3章动态规划(一)[背包问题] 基本思想: 动态规划算法与分治法类似,其基本思想也是将待求解问题分解成若干个子问题,但是经分解得到的子问题往往不是互相独立的.不同子问题的数目常常只有多项式量级.在用 ...

  9. ADPRL - 近似动态规划和强化学习 - Note 7 - Approximate Dynamic Programming

    Note 7 - 近似动态规划 Approximate Dynamic Programming 7. 近似动态规划 (Approximate Dynamic Programming) 7.1 近似架构 ...

最新文章

  1. java 集合 接口_Java集合之Collection接口
  2. 【连载】高效人士的116个IT秘诀(第2版)——秘诀24为你的时间建一个构造图
  3. iphone怎么长截屏_新iPhone又要为中国定制?除了价格,还有这些惊喜
  4. 电脑开机进入桌面很慢的解决办法
  5. Visual Studio 2015 自动生成 的大文件xxx.vc.db的删除问题
  6. Linux命令笔记(一)
  7. 修复IE下列表 li 的阶梯Bug
  8. airflow连接mysql错误_安装Airflow遇到的问题以及解决过程
  9. 商城网站该如何选择虚拟主机
  10. 上市公司与不上市公司的区别
  11. Java获取网络IP
  12. 【Arcpy】Python in ArcGIS
  13. java中关闭数据库连接_在Java中关闭数据库连接
  14. 高温差热分析仪(高温热重分析仪)
  15. vscode格式化css文件,css文件排版
  16. 基于Java的飞机大战的课程设计与实现
  17. [入门篇]Linux操作系统fork子进程的创建以及进程的状态 超超超详解!!!我不允许有人错过!!!
  18. App.config提示错误“配置系统未能初始化”
  19. ibm service guide
  20. 黎明纪元-3D魔幻世界角色扮演游戏

热门文章

  1. Windows安装配置Python Scrapy环境
  2. 文件/文件夹管理与操作命令
  3. 功率放大器设计方案(包含原理图+PCB+BOM表)
  4. python共享单车数据分析_数据分析_共享单车骑行时间分析-zeropython
  5. word调整页脚距离 顶端和低端的距离(叫页边距)
  6. 软考对找工作有帮助吗?
  7. NG-ZORRO1.x自定义主题
  8. 启动radius服务器报错
  9. 首份数据基础白皮书发布,直指未来基础设施痛点
  10. 【入门Audio音频】必了解:Audio音频格式介绍