传送门:https://www.luogu.org/problemnew/show/P1091
正反各求一遍最长上升子序列,然后相加求最大值
#include<cstdio> #include<algorithm> using namespace std; inline int read() { static char ch; while((ch = getchar()) < '0' || ch > '9'); int ret = ch -48; while((ch = getchar()) >= '0' && ch <= '9') ret = ret * 10 + ch - 48; return ret; } int n,a[105],f1[105],f2[105],ans; int main() { n = read(); for(int i = 1;i <= n;i++) a[i] = read(); a[0] = a[n+1] = 0; for(int i = 1;i <= n;i++) for(int j = 0;j < i;j++) if(a[i] > a[j]) f1[i] = max(f1[i] ,f1[j] + 1); for(int i = n;i > 0;i--) for(int j = n+1;j > i;j--) if(a[i] > a[j]) f2[i] = max(f2[i],f2[j]+1); for(int i = 1;i <= n;i++) ans = max(f1[i]+f2[i]-1,ans); //重叠的要减掉 printf("%d ",n-ans); return 0; }