https://jzoj.net/senior/#main/show/1003
简单dp,O(n^2)
#include<iostream> #include<cstring> #include<cstdio> using namespace std; inline int rd(){ int ret=0,f=1;char c; while(c=getchar(),!isdigit(c))f=c=='-'?-1:1; while(isdigit(c))ret=ret*10+c-'0',c=getchar(); return ret*f; } const int MAXN=1024; int n,T; int h[MAXN],f[MAXN][2]; void init(){ memset(f,0,sizeof(f)); h[0]=-1; } void solve(){ int ans=0; for(int i=1;i<=n;i++) h[i]=rd(); for(int i=1;i<=n;i++){ for(int j=0;j<i;j++){ if(h[j]==h[i]) continue; if(h[j]<h[i]) f[i][1]=max(f[i][1],f[j][0]+1); else f[i][0]=max(f[i][0],f[j][1]+1); ans=max(ans,max(f[i][0],f[i][1])); } } printf("%d ",ans); } int main(){ while(n=rd()){ init(); solve(); } return 0; }