bzoj1996: [Hnoi2010]chorus 合唱队

题目链接

1996: [Hnoi2010]chorus 合唱队

题解

区间dp
设dp[i][j][0/1] 表示可以构成i到j
0表示上次匹配在左,1表示在右
转移显然

代码

#include<cstdio>
#include<algorithm>
#define mod 19650827
const int maxn = 1007; 
inline int read() {
    int x = 0,f = 1;
    char c = getchar();
    while(c < '0' || c > '9') {if(c == '-')f = -1;c = getchar();} 
    while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = getchar();
    return x * f;
}
int n;
int dp[maxn][maxn][2],a[maxn]; 
int main() { 
     n = read(); 
    for(int i = 1;i <= n;++ i) a[i] = read(); 
    for(int i = 1;i <= n;++ i) dp[i][i][0]  = 1; 
    for(int i = n;i >= 1;-- i) 
        for(int j = i + 1;j <= n;++ j) { 
            if(a[j] > a[i]) dp[i][j][1] += dp[i][j - 1][0]; 
            if(a[j] > a[j - 1]) dp[i][j][1] += dp[i][j - 1][1]; 
            if(a[i] < a[j]) dp[i][j][0] += dp[i + 1][j][1]; 
            if(a[i] < a[i + 1]) dp[i][j][0] += dp[i + 1][j][0]; 
            dp[i][j][1] %= mod;
            dp[i][j][0] %= mod;
        } 
    printf("%d\n",(dp[1][n][0] + dp[1][n][1]) % mod);
    return 0;
}

posted @ 2018-05-08 15:43  zzzzx  阅读(139)  评论(0编辑  收藏  举报