Description
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 <= 2000
测试输入 | 期待的输出 | 时间限制 | 内存限制 | 额外进程 | |
---|---|---|---|---|---|
测试用例 1 | 以文本方式显示
| 以文本方式显示
| 1秒 | 64M | 0 |
测试用例 2 | 以文本方式显示
| 以文本方式显示
| 1秒 | 64M | 0 |
测试用例 3 | 以文本方式显示
| 以文本方式显示
| 1秒 | 64M | 0 |
思路
翻译一下题目的要求:
1.给定了最底箭头的的尾和所有箭头的头。
2.要求箭头的尾只能和前一箭头的头和尾对齐(由此我们可以确定箭头尾部的最大坐标head的最大值,有些同学说没有给出范围,但实际可以自己找出来。
3.数据很大,要用long long储存,每次运算都应取模防止溢出
使用dp[i][j]表示第i个箭头尾部在j时的方案数。初始化dp[1][a[0]]=1.
外层循环从第2个箭头开始,内层循环从1到max。有以下几种情况:
i,i-1头部在i尾部同一侧,此时i尾和i-1尾必须相连,即dp[i][j]=dp[i-1][j]
i尾和i-1头坐标相同,已经固定。看箭头朝向,上右下左,从j+1累加到max;上左下右,从1到j-1。
其他情况不可能满足要求。大家可以画几张图方便理解
当时写了个 sum += dp[i - 1][k] % MOD;还一直没意识到问题,想着明明取模了还WA了6个。犯了个zz错误
代码
#include <stdio.h>
#define N 2005
#define M 2147483647
long long dp[N][N] = {0};
// 第i块,尾端置于j时的方案
int main(int argc, char const *argv[])
{
int n, i, j, k, a[N],max=0;
long long ans = 0;
scanf("%d", &n);
for (i = 0; i <= n; i++)
{
scanf("%d", &a[i]);
if(a[i]>max) max=a[i];
}
dp[1][a[0]] = 1;
//dp[1][a[1]] = 1;
if (n == 1)
{
printf("1\n");
return 0;
}
for (i = 2; i <= n; i++)
{
int small = (a[i - 1] > a[i]) ? a[i] : a[i-1];
int big = (a[i - 1] > a[i]) ? a[i-1] : a[i];
for (j = 1; j <= max; j++)
{
if (j > big || j < small)
{
// 上下同尾
dp[i][j] = dp[i - 1][j];
}
else if (j == a[i - 1])
{
// 上尾下首
long long sum = 0;
if (a[i] > j) // 上右下左
for (k = j + 1; k <= max; k++){
sum += dp[i - 1][k];
sum%=M;
}
else if(a[i] < j)// 上左下右
for (k = 1; k < j; k++)
sum += dp[i - 1][k];
sum%=M;
dp[i][j] = sum;
}
}
}
for (i = 1; i <= max; i++){
ans += dp[n][i] ;
ans%=M;
}
printf("%lld\n", ans);
return 0;
}