dp[i][j][0/1]表示考虑i...j,最后一个放左边还是放右边的方案数。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxn 1050 #define mod 19650827 using namespace std; int n,h[maxn],dp[maxn][maxn][2]; int main() { scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d",&h[i]); for (int i=1;i<=n;i++) dp[i][i][0]=1; for (int l=2;l<=n;l++) for (int i=1;i+l-1<=n;i++) { int j=i+l-1; if (h[i]<h[i+1]) dp[i][j][0]=(dp[i][j][0]+dp[i+1][j][0])%mod; if (h[i]<h[j]) dp[i][j][0]=(dp[i][j][0]+dp[i+1][j][1])%mod; if (h[j]>h[j-1]) dp[i][j][1]=(dp[i][j][1]+dp[i][j-1][1])%mod; if (h[j]>h[i]) dp[i][j][1]=(dp[i][j][1]+dp[i][j-1][0])%mod; } printf("%d\n",(dp[1][n][0]+dp[1][n][1])%mod); return 0; }