HDU 5151 Sit sit sit(区间DP)

HDU 5151 Sit sit sit

题目链接

区间DP+组合计数问题,转移方程为,每次选当前区间最后一个放的位置,然后乘上组合数C[区间长度][左区间长度]

代码:

#include <cstdio>
#include <cstring>

typedef long long ll;

const ll MOD = 1000000007;

const int N = 105;

int n, a[N];
ll dp[N][N], C[N][N];

int main() {
    C[1][0] = C[1][1] = 1;
    for (int i = 2; i <= 100; i++) {
	C[i][0] = C[i][i] = 1;
	for (int j = 1; j < i; j++) {
	    C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD;
	}
    }
    while (~scanf("%d", &n)) {
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
	memset(dp, 0, sizeof(dp));
	for (int i = 1; i <= n; i++) dp[i][i] = 1;
	for (int len = 1; len < n; len++) {
	    for (int l = 1; l + len <= n; l++) {
		int r = l + len;
		for (int k = l; k <= r; k++) {
		    if (l == k) dp[l][r] = (dp[l][r] + dp[k + 1][r]) % MOD;
		    else if (r == k) dp[l][r] = (dp[l][r] + dp[l][k - 1]) % MOD;
		    else if (a[k - 1] == a[k + 1]) dp[l][r] = (dp[l][r] + (dp[l][k - 1] * dp[k + 1][r] % MOD * C[r - l][k - l]) % MOD) % MOD;
		}
	    }
	}
	printf("%lld\n", dp[1][n]);
    }
    return 0;
}
时间: 2024-10-12 23:22:43

HDU 5151 Sit sit sit(区间DP)的相关文章

HDU 4960 Another OCD Patient 区间dp

区间dp.. T^T一直感觉是n^3,看了题解看来是数据水了么.. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string.h> #define ll long long #define inf 1e8 inline int min(int a, int b){return a<b?a:b;} inline void rdl(ll

HDU 2476 String painter(区间DP啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2476 Problem Description There are two strings A and B with equal length. Both strings are made up of lower case letters. Now you have a powerful string painter. With the help of the painter, you can cha

HDU 2476 String painter (区间DP)

题意:给出两个串a和b,一次只能将一个区间刷一次,问最少几次能让a=b 思路:首先考虑最坏的情况,就是先将一个空白字符串刷成b需要的次数,直接区间DP[i][j]表示i到j的最小次数. 再考虑把a变成b的次数 ans[i]:a从(0,i)变成b(0,i)所需的最小次数 初始化ans[i]=dp[0][i] 如果a[i]==b[i],则ans[i]=ans[i-1]; 由小区间更新到大区间 1 //#pragma comment(linker, "/STACK:167772160")//

hdu 5115 Dire Wolf(区间dp)

Problem Description Dire wolves, also known as Dark wolves, are extraordinarily large and powerful wolves. Many, if not all, Dire Wolves appear to originate from Draenor. Dire wolves look like normal wolves, but these creatures are of nearly twice th

HDU 4960 Another OCD Patient(区间dp记忆化搜索)

题目大意:给你一串数字让你判断经过若干次合并,使得这个数字串变成回文串的最小成本是多少.第一行是数字串,第二行是合并连续i个数字的成本是多少. 解题思路:区间dp,可以进行记忆化搜索,如果左边比右边和大那么右边一定是小了,右边比左边大那么左边一定小了.因为保证有解.具体不太好说,直接看代码吧. Another OCD Patient Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe

HDU 5115 Dire Wolf ——(区间DP)

比赛的时候以为很难,其实就是一个区间DP= =..思路见:点我. 区间DP一定要记住先枚举区间长度啊= =~!因为区间dp都是由短的区间更新长的区间的,所以先把短的区间更新完.. 代码如下: 1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 using namespace std; 5 const int N = 200 + 5; 6 const int inf = 0x3f3f3f3

HDU 4570 Multi-bit Trie(区间dp)

Multi-bit Trie Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 539    Accepted Submission(s): 214 Problem Description IP lookup is one of the key functions of routers for packets forwarding and

HDU ACM 4597 Play Game -&gt;区间DP+记忆化搜索

分析:两个人都足够聪明,因此每个阶段都拿最大的.dp[sa][ea][sb][eb]分别表示区间1的开始为sa,结束为ea,区间2的开始为sb,结束为eb时能拿到的最大值.之后分别从四个方向上拿,是个搜索的过程. [cpp] view plaincopyprint? #include<iostream> using namespace std; int dp[25][25][25][25];  //dp[sa][ea][sb][eb],分别表示区间1的开始,结束,区间2的开始,结束 int a

hdu 5115 Dire Wolf【区间DP】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5115 题意: 有n只狼,每只狼有两种属性,一种攻击力一种附加值,我们没杀一只狼,那么我们受到的伤害值为这只狼的攻击值与它旁边的两只狼的附加值的和,求把所有狼都杀光受到的最小的伤害值. 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <

HDU 2476 String painter(区间dp)

String painter Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2171    Accepted Submission(s): 956 Problem Description There are two strings A and B with equal length. Both strings are made up