手画一下样例就有点感觉了。
一开始定了二维的状态,发现答案跑不对,后来意识到需要第三维,表示当前区间最后一个填的数是最左边的还是最右边的,然后就可以转移了。
/* 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;
}