HDU 1087 Super Jumping! Jumping! Jumping! ——(LIS变形)

  和之前那个长方体最大高度是换汤不换药的题目。每次找之前最大的一个能接的接上即可。代码如下:

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <string.h>
 4 using namespace std;
 5 const int inf = 0x3f3f3f3f;
 6 const int N = 1000 + 5;
 7 
 8 int a[N];
 9 int dp[N];
10 
11 int main()
12 {
13     int n;
14     while(scanf("%d",&n) == 1 && n)
15     {
16         for(int i=1;i<=n;i++) scanf("%d",a+i);
17         memset(dp,0,sizeof dp);
18         for(int i=1;i<=n;i++)
19         {
20             int pos = -1;
21             for(int j=1;j<i;j++)
22             {
23                 if(a[j] >= a[i]) continue;
24                 if(pos == -1) pos = j;
25                 else if(dp[pos] < dp[j]) pos = j;
26             }
27             if(pos == -1) dp[i] = a[i];
28             else dp[i] = dp[pos] + a[i];
29         }
30         printf("%d
",*max_element(dp+1,dp+1+n));
31     }
32     return 0;
33 }
原文地址:https://www.cnblogs.com/zzyDS/p/6400175.html