方向标Directional Sign

题目信息

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).

输入

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.

输出

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

1 ≤ N ≤ 2000

测试样例

测试样例1

2
2 3 1
1

测试样例2

5
2 6 5 1 4 3
6

测试样例3

20
3 21 10 15 6 9 2 5 20 13 17 19 14 7 11 18 16 12 1 8 4
1887

解答

#include <iostream>
#include <vector>
#include <algorithm>

#define ll long long
#define mod 2147483647

using namespace std;

ll dp[2005][2005];
ll head[2005];

ll max(ll a, ll b)
{
    if (a > b)
        return a;
    else
        return b;
}

ll min(ll a, ll b)
{
    if (a > b)
        return b;
    else
        return a;
}

int main()
{
    //freopen("E://test.txt", "r", stdin);
    ll N;
    cin >> N;
    cin >> head[0];
    dp[1][head[0]] = 1;//初始化
    for (int i = 1; i <= N; i++)
    {
        cin >> head[i];
        for (int j = 0; j < N + 2; j++)
        {
            ll left = min(head[i - 1], j);
            ll right = max(head[i - 1], j);
            if (head[i] <= left)
            {
                dp[i][right] = (dp[i - 1][j] + dp[i][right]) % mod;
            }
            if (head[i] >= right)
            {
                dp[i][left] = (dp[i - 1][j] + dp[i][left]) % mod;
            }
            if (head[i] < right && head[i] > left)
            {
                dp[i][left] = (dp[i - 1][j] + dp[i][left]) % mod;
                dp[i][right] = (dp[i - 1][j] + dp[i][right]) % mod;
            }
        }
    }
    ll ans = 0;
    for (int i = 0; i < N + 2; i++)
    {
        ans = (ans + dp[N][i]) % mod;
    }
    cout << ans << endl;
    return 0;
}
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zhj12399

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

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

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

打赏作者

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

抵扣说明:

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

余额充值