动态规划找最长上升子序列,正反遍历一遍序列即可~
#include<bits/stdc++.h> using namespace std; const int maxn=10010; int N; int a[maxn]; int l[maxn]; int r[maxn]; int main () { scanf ("%d",&N); for (int i=0;i<N;i++) scanf ("%d",&a[i]); int maxLength=0; int maxIndex; int maxValue; int mincha; for (int i=1;i<N;i++) { for (int j=i-1;j>=0;j--) if (a[j]<a[i]) l[i]=max(l[i],l[j]+1); } for (int i=N-2;i>=0;i--) { for (int j=i+1;j<N;j++) if (a[j]<a[i]) r[i]=max(r[i],r[j]+1); } for (int i=1;i<N;i++) { if (l[i]==0||r[i]==0) continue; int len=l[i]+r[i]+1; if (len>maxLength) { maxLength=len; maxIndex=i; mincha=abs(l[i]-r[i]); } else if (len==maxLength&&abs(l[i]-r[i])<mincha) { maxIndex=i; mincha=abs(l[i]-r[i]); } } if (maxLength==0) printf ("No peak shape"); else printf ("%d %d %d",maxLength,maxIndex+1,a[maxIndex]); return 0; }