UVALive 7276 Wooden Signs (DP)

Wooden Signs

题目链接:

http://acm.hust.edu.cn/vjudge/contest/127406#problem/E

Description


http://7xjob4.com1.z0.glb.clouddn.com/0f10204481da21e62f8c145939e5828e

Input


The input file contains several test cases, each of them as described below.
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.
Constraints:
1 ≤ N < 2000 Number of Arrows.

Output


For each test case, the output has a single line with the number (modulo 2
31 − 1 = 2147483647) of
distinct signs that can be described by the given permutation.

Sample Input


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

Sample Output


6
1
1887


题意:


木匠要做N个路标并把它们钉在一起(如图).
每个路标可以朝左也可以朝右,但是每个路标的起点要跟下一层路标的起点或终点重合. 重合位置必须有面积覆盖,不能像231右图那种只重合一个点.
现在他不记得这些路标各自的朝向了. 但是他记得N+1个值,其中第i个值是第i个路标的起点坐标. (第N+1为最后一块的终点).
求可能满足条件的路标有多少种情况. (区别在于各自的朝向)(第一块路标一定朝右)


题解:


考虑每块路标的终点:
若第i块路标的坐标区间为[L, R], 第i+1块路标的终点为Xi+1.
那么基于第i块路标来摆放第i+1块的方式有两种:
①第i+1块的起点在L处(朝右). 这要求 Xi+1 > L .
②第i+1块的起点在R处(朝左). 这要求 Xi+1 < R .
基于以上递归关系来进行动态规划:
dp[i][0][j]: 表示第i块板子的区间为 [j, Xi] 的方案数(朝右).
dp[i][1][j]: 表示第i块板子的区间为 [Xi, j] 的方案数(朝左).
再根据"我为人人"的思想从第 i 块拓展到第 i+1 块即可.


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <list>
#define LL long long
#define eps 1e-8
#define maxn 2016
#define mod 2147483647LL
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;

LL dp[maxn][2][maxn];
int n, arrow[maxn];

int main(int argc, char const *argv[])
{
    //IN;

    while(scanf("%d", &n) != EOF)
    {
        int leftmost; scanf("%d", &leftmost);
        for(int i=1; i<=n; i++)
            scanf("%d", &arrow[i]);
        memset(dp, 0, sizeof(dp));

        dp[1][0][leftmost] = 1;
        for(int i=1; i<n; i++) {
            for(int j=1; j<=n+1; j++) {
                if(dp[i][0][j]) {
                    if(arrow[i+1] > j) dp[i+1][0][j] = (dp[i+1][0][j] + dp[i][0][j]) % mod;
                    if(arrow[i+1] < arrow[i]) dp[i+1][1][arrow[i]] = (dp[i+1][1][arrow[i]] + dp[i][0][j]) % mod;
                }
                if(dp[i][1][j]) {
                    if(arrow[i+1] < j) dp[i+1][1][j] = (dp[i+1][1][j] + dp[i][1][j]) % mod;
                    if(arrow[i+1] > arrow[i]) dp[i+1][0][arrow[i]] = (dp[i+1][0][arrow[i]] + dp[i][1][j]) % mod;
                }
            }
        }

        LL ans = 0;
        for(int j=1; j<=n+1; j++) {
            ans = (ans + dp[n][0][j]) % mod;
            ans = (ans + dp[n][1][j]) % mod;
        }

        printf("%lld\n", ans);
    }

    return 0;
}

转载于:https://www.cnblogs.com/Sunshine-tcf/p/5769668.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值