题目:https://jzoj.net/senior/#main/show/1003
n^2 的话递推就可以啦。
代码如下:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int const maxn=1005; int n,h[maxn],f[maxn],g[maxn],ans; int main() { while(1) { scanf("%d",&n); if(!n)return 0; memset(f,0,sizeof f); memset(g,0,sizeof g); for(int i=1;i<=n;i++)scanf("%d",&h[i]); f[n]=1; g[n]=1; ans=1;// for(int i=n;i;i--) for(int j=i+1;j<=n;j++) { if(h[j]>h[i])f[i]=max(f[i],g[j]+1); else if(h[j]<h[i])g[i]=max(g[i],f[j]+1); ans=max(ans,g[i]); } printf("%d ",ans); } }