n2 过不了惨啊
70分做法
f[i][0] 表示第 i 个作为高的,的最优解
f[i][0] 表示第 i 个作为低的,的最优解
(且第 i 个一定选)
那么
f[i+1][1]=max(f[j][0])+1,i>=j>=1,h[j]>h[i+1],
f[i+1][0]=max(f[j][1])+1,i>=j>=1,h[j]<h[i+1],
——代码
1 #include <cstdio> 2 #include <algorithm> 3 4 const int MAXN = 100001, INF = ~(1 << 31); 5 int n, ans, max; 6 int a[MAXN], f[MAXN][2]; 7 8 inline int Max(int x, int y) 9 { 10 return x > y ? x : y; 11 } 12 13 int main() 14 { 15 //freopen("flower.in", "r", stdin); 16 //freopen("flower.out", "w", stdout); 17 int i, j; 18 scanf("%d", &n); 19 for(i = 1; i <= n; i++) scanf("%d", &a[i]); 20 for(i = 1; i <= n; i++) 21 { 22 max = 0; 23 for(j = 1; j < i; j++) 24 if(a[j] < a[i] && f[j][1] > max) 25 max = f[j][1]; 26 f[i][0] = max + 1; 27 ans = Max(ans, f[i][0]); 28 max = 0; 29 for(j = 1; j < i; j++) 30 if(a[j] > a[i] && f[j][0] > max) 31 max = f[j][0]; 32 f[i][1] = max + 1; 33 ans = Max(ans, f[i][1]); 34 } 35 printf("%d\n", ans); 36 return 0; 37 }