【BZOJ1996】[Hnoi2010]chorus 合唱队【区间DP】

【题目链接】

手画一下样例就有点感觉了。

一开始定了二维的状态,发现答案跑不对,后来意识到需要第三维,表示当前区间最后一个填的数是最左边的还是最右边的,然后就可以转移了。

/* Telekinetic Forest Guard */
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn = 1005, p = 19650827;

int n, num[maxn], dp[maxn][maxn][2];

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

inline int dfs(int l, int r, bool f) {
	if(l == r) return f;
	if(~dp[l][r][f]) return dp[l][r][f];
	int res = 0;
	if(!f) {
		if(num[l] < num[l + 1]) (res += dfs(l + 1, r, f)) %= p;
		if(num[l] < num[r]) (res += dfs(l + 1, r, f ^ 1)) %= p;
	} else {
		if(num[r - 1] < num[r]) (res += dfs(l, r - 1, f)) %= p;
		if(num[l] < num[r]) (res += dfs(l, r - 1, f ^ 1)) %= p;
	}
	return dp[l][r][f] = res;
}

int main() {
	n = iread();
	for(int i = 1; i <= n; i++) num[i] = iread();
	memset(dp, -1, sizeof(dp));
	printf("%d\n", (dfs(1, n, 0) + dfs(1, n, 1)) % p);
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值