HDU 5151 Sit sit sit (区间DP)

Sit sit sit

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 266    Accepted Submission(s): 125


Problem Description
There are N chairs in a row and and no one on the chairs.Each chair is blue or red.Then comes N students one by one numbered 1,2,3,..,N.For each student, he will find an empty chair to sit on.He won’t sit on the chair if the chair is satisfied the following three conditions.

1.The chair has both left and right adjacent chairs.
2.The left and right adjacent chairs are not empty.
3.The left and right adjacent chairs’ color are different.
If the current student can’t find a chair to sit on, he will go away.

For each student, he may have many choices of chairs to sit on. Your task is to find the number of distinct situations that all students have sat on a chair. As the answer can be rather large, find remainder after dividing the number by 1000000007(109+7) .
 

Input
There are several test cases.
In each test case:
The first line contains a integer N(1N100) .The second line contains N integers.Each integer is either 0 or 1.Integer 0 means blue and 1 means red.
 

Output
For each test case, output the remainder of division of the resulting number by 1000000007(109+7) .
 

Sample Input
3 1 0 0 4 1 0 0 1
 

Sample Output
4 8
 

Source

题意:一共有并排N个椅子, N个学生依次去坐,同时满足3个条件就不能坐下去:1,该椅子不在最左,不在最右,2,该椅子左右都有人坐了,3,左右的椅子不同颜色,求最后N个人都能坐下去,有多少不同的情况.

思路:dp[i][j]表示排完区间[i,j]的种类数,dp[i][j] = SUM ( dp[i][k-1]*dp[k+1][j]* C[j-i][i-k] )        

其中v[k-1]==v[k+1],保证最后的那个最为合法。乘以组合数的原因是在j-i个人中决定坐前一个区间的人(选完之后,他们是有序的)。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
#define INF 1000000000
#define mod 1000000007
const int N = 110;
LL dp[N][N],C[N][N];
int a[N];
void init(int n)
{
    for (int i = 0; i <= n; i++)
    {
        C[i][0] = C[i][i] = 1;
        for (int j = 1; j < i; j++)
            C[i][j] = (C[i-1][j-1] + C[i-1][j]) % mod;
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        init(N);
        memset(dp,0,sizeof(dp));
        for(int i=0; i<n ; i++)
            scanf("%d",&a[i]);
        for(int i=0; i<n; i++)
            dp[i][i] = 1;
        for(int i=n-1; i>=0; i--)
            for(int j=i+1; j<n; j++)
                for(int k=i; k<=j; k++)//这里要取到边界
                {
                    if(i==k)//这里是坐在两端的情况
                    {
                        dp[i][j] = (dp[i][j] + dp[k+1][j]) % mod;
                    }
                    else if(k==j)
                    {
                        dp[i][j] = (dp[i][j] + dp[i][k-1]) % mod;
                    }
                    else if(a[k-1]==a[k+1])//这是当最后一个人是 K 时候做的情况
                    {
                        dp[i][j] = (dp[i][j] + dp[i][k-1] * dp[k+1][j] % mod * C[j-i][k-i] % mod) % mod;
                    }
                }
        printf("%I64d\n",dp[0][n-1]);
    }
    return 0;
}
View Code
posted @ 2015-07-27 15:46  Doli  阅读(144)  评论(0编辑  收藏  举报