【DP】 HDOJ 5151 Sit sit sit

可以看成区间DP,每次在l,r里选一个最小的数,将区间分成两半,统计方案数。。。。

#include <iostream>
#include <queue> 
#include <stack> 
#include <map> 
#include <set> 
#include <bitset> 
#include <cstdio> 
#include <algorithm> 
#include <cstring> 
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 105
#define maxm 2000005
#define eps 1e-10
#define mod 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
//#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
// head

int a[maxn];
LL dp[maxn][maxn];
LL c[maxn][maxn];
int n;

void init(void)
{
	c[0][0] = 1;
	c[0][1] = 0;
	for(int i = 1; i <= 100; i++) {
		c[i][0] = 1;
		for(int j = 1; j < i; j++) c[i][j] = (c[i-1][j-1] + c[i-1][j]) % mod;
		c[i][i] = 1;
	}
}

void read(void)
{
	for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
}

LL dfs(int L, int R)
{
	if(dp[L][R] != -1) return dp[L][R];
	if(L > R) return 1;
	if(L == R) {
		if(L == 1 || R == n || a[L-1] == a[L+1]) return dp[L][R] = 1;
		else return dp[L][R] = 0;
	}
	LL res = 0;
	for(int k = L; k <= R; k++)
		res = (res + c[R - L][k - L] * dfs(L, k - 1) % mod * dfs(k + 1, R) % mod) % mod;
	return dp[L][R] = res;
}

void work(void)
{
	memset(dp, -1, sizeof dp);
	printf("%I64d\n", dfs(1, n));
}

int main(void)
{
	init();
	while(scanf("%d", &n)!=EOF) {
		read();
		work();
	}	

	return 0;
}


你可能感兴趣的:(HDU)