传送门

 

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 }
View Code

相关文章:

  • 2021-06-10
  • 2021-10-30
  • 2021-09-08
  • 2021-12-11
  • 2021-11-05
猜你喜欢
  • 2021-09-06
  • 2021-07-13
  • 2021-09-26
  • 2021-06-02
  • 2022-02-27
  • 2021-09-09
相关资源
相似解决方案