27. 方向标

文章讨论如何根据编码规则计算基于给定数字序列的可制作方向指示牌数量,考虑箭头方向和重叠条件,要求结果对2147483647取模。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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. 2↵
  2. 2 3 1↵
以文本方式显示
  1. 1↵
1秒64M0
测试用例 2以文本方式显示
  1. 5↵
  2. 2 6 5 1 4 3↵
以文本方式显示
  1. 6↵
1秒64M0
测试用例 3以文本方式显示
  1. 20↵
  2. 3 21 10 15 6 9 2 5 20 13 17 19 14 7 11 18 16 12 1 8 4↵
以文本方式显示
  1. 1887↵
1秒64M0
思路

翻译一下题目的要求:
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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北辰2023

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值