题目链接

https://www.lydsy.com/JudgeOnline/problem.php?id=1996

题解

一个简单的区间dp,f[i][j]f[i][j]代表[i,j][i,j]区间,最后一个插在左边的方案数,g[i][j]g[i][j]则表示插在右边的方案数,这个很好转移,最后答案就是f[i][j]+g[i][j]f[i][j]+g[i][j]

代码

#include <cstdio>
 
int read()
{
  int x=0,f=1;
  char ch=getchar();
  while((ch<'0')||(ch>'9'))
    {
      if(ch=='-')
        {
          f=-f;
        }
      ch=getchar();
    }
  while((ch>='0')&&(ch<='9'))
    {
      x=x*10+ch-'0';
      ch=getchar();
    }
  return x*f;
}
 
const int maxn=1000;
const int mod=19650827;
 
int n,h[maxn+10],f[maxn+10][maxn+10],g[maxn+10][maxn+10];
 
int main()
{
  n=read();
  for(int i=1; i<=n; ++i)
    {
      h[i]=read();
    }
  for(int i=1; i<=n; ++i)
    {
      f[i][i]=1;
    }
  for(int len=2; len<=n; ++len)
    {
      for(int i=1; i+len-1<=n; ++i)
        {
          int j=i+len-1;
          f[i][j]=(h[i]<h[i+1])*f[i+1][j]+(h[i]<h[j])*g[i+1][j];
          if(f[i][j]>=mod)
            {
              f[i][j]-=mod;
            }
          g[i][j]=(h[j]>h[j-1])*g[i][j-1]+(h[j]>h[i])*f[i][j-1];
          if(g[i][j]>=mod)
            {
              g[i][j]-=mod;
            }
        }
    }
  int ans=f[1][n]+g[1][n];
  if(ans>=mod)
    {
      ans-=mod;
    }
  printf("%d\n",ans);
  return 0;
}