登山(LIS变形)

登山(LIS变形)

注意读题:不连续两个相同海拔,所以要么严格递增,要么严格递减

AC_Code

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <string>
 5 #include <cmath>
 6 #include <cstdlib>
 7 #include <queue>
 8 #include <vector>
 9 #include <algorithm>
10 #include <ctime>
11 #include <set>
12 #include <map>
13 using namespace std;
14 typedef long long ll;
15 const int maxn = 1010;
16 
17 int main()
18 {
19     int dp1[maxn]={0}, dp2[maxn]={0}, a[maxn], dp[maxn]={0};
20     int n;
21     scanf("%d",&n);
22     for(int i=1;i<=n;i++){
23         scanf("%d",&a[i]);
24     }
25 
26     for(int i=1;i<=n;i++){
27         for(int j=1;j<i;j++){
28             if( dp1[i]<dp1[j]+1 && a[i]>a[j] ){
29                 dp1[i] = dp1[j]+1;
30             }
31         }
32     }
33 
34     for(int i=n;i>=1;i--){
35         for(int j=n;j>i;j--){
36             if( dp2[i]<dp2[j]+1 && a[i]>a[j] ){
37                 dp2[i] = dp2[j]+1;
38             }
39         }
40     }
41 
42     int maxx=-1;
43     for(int i=1;i<=n;i++){
44         dp[i] = (dp1[i]+1)+(dp2[i]+1)-1;
45         maxx = max(maxx, dp[i]);
46     }
47     printf("%d\n",maxx);
48     return 0;
49 }

猜你喜欢

转载自www.cnblogs.com/wsy107316/p/12239713.html
lis