Sit sit sit
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 329 Accepted Submission(s): 160
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) .
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(1≤N≤100) .The second line contains N integers.Each integer is either 0 or 1.Integer 0 means blue and 1 means red.
In each test case:
The first line contains a integer N(1≤N≤100) .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
1:这张椅子左右也有椅子(也就是说不是最左边或者最右边的椅子)
2:这张椅子左右都有人
3:这张椅子左右两张椅子颜色不同
比如有三张椅子 1 0 1(红,蓝,红)只能按照(1 2 3 )(2 1 3)(2 3 1)(3 2 1)这个顺序坐,(1 3 2)和(3 1 2)因为2号椅子满足上述三个条件而无法坐下
问一共有几种坐法?
思路:看出是DP,不过没能想出如何表示状态以及如何转移。
具体可以看这位大神的博客,写的十分详细。
预处理c数组表示100以内的所有组合数
区间dp方程在转移的时候注意要先枚举区间长度,因为不管是dp[i][j]+=dp[i+1][j]还是dp[i][j]+=dp[i][j-1]都必须是预先处理过的,也就是说当你在计算区间长度为len时,我们需要用到区间长度为len-1的值,那么此时len-1的区间值必须是已经更新过的了。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 110
#define mod 1000000007
long long c[N][N];
int a[N];
long long dp[N][N];
void init()
{
for(int i=1; i<=100; 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;
init();
while(~scanf("%d",&n))
{
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
memset(dp,0,sizeof(dp));
for(int i=0; i<n; i++)
dp[i][i]=1;
for(int i=0; i<n-1; i++)
dp[i][i+1]=2;
for(int len=2; len<n; len++)
for(int i=0; i+len<n; i++)
{
int j=i+len;
dp[i][j]=(dp[i][j]+dp[i+1][j])%mod;
dp[i][j]=(dp[i][j]+dp[i][j-1])%mod;
for(int k=i+1; k<j; k++)
if(a[k-1]==a[k+1])
dp[i][j]=(dp[i][j]+dp[i][k-1]*dp[k+1][j]%mod*c[j-i][k-i]%mod)%mod;
}
printf("%lld\n",dp[0][n-1]);
}
return 0;
}