kuangbin专题十二 HDU1087 Super Jumping! Jumping! Jumping! (LIS)

Super Jumping! Jumping! Jumping!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 50078    Accepted Submission(s): 23221


Problem Description
Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now.



The game can be played by two or more than two players. It consists of a chessboard(棋盘)and some chessmen(棋子), and all chessmen are marked by a positive integer or “start” or “end”. The player starts from start-point and must jumps into end-point finally. In the course of jumping, the player will visit the chessmen in the path, but everyone must jumps from one chessman to another absolutely bigger (you can assume start-point is a minimum and end-point is a maximum.). And all players cannot go backwards. One jumping can go from a chessman to next, also can go across many chessmen, and even you can straightly get to end-point from start-point. Of course you get zero point in this situation. A player is a winner if and only if he can get a bigger score according to his jumping solution. Note that your score comes from the sum of value on the chessmen in you jumping path.
Your task is to output the maximum value according to the given chessmen list.
 
Input
Input contains multiple test cases. Each test case is described in a line as follow:
N value_1 value_2 …value_N
It is guarantied that N is not more than 1000 and all value_i are in the range of 32-int.
A test case starting with 0 terminates the input and this test case is not to be processed.
 
Output
For each case, print the maximum according to rules, and one line one case.
 
Sample Input
3 1 3 2 4 1 2 3 4 4 3 3 2 1 0
 
Sample Output
4 10 3
 
 
 

题目大意:给出一个序列,求严格上升子序列的最大和。

看到题目,发现是之前做过的题目,但是读了题之后,发现不是普通的,(可能是状态不好,就不想做了)。dp[i] 表示 以 i 结尾的最大和。

状态转移方程:dp[i] = max(a[i], max{dp[j] | 0 <= j < i, a[j] < a[i])} + a[i]

 

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <math.h>
 4 #include <string.h>
 5 #include <stdlib.h>
 6 #include <string>
 7 #include <vector>
 8 #include <set>
 9 #include <map>
10 #include <queue>
11 #include <algorithm>
12 #include <sstream>
13 #include <stack>
14 using namespace std;
15 #define mem(a,b) memset((a),(b),sizeof(a))
16 #define mp make_pair
17 #define pb push_back
18 #define fi first
19 #define se second
20 #define sz(x) (int)x.size()
21 #define all(x) x.begin(),x.end()
22 #define forn(i, x, n) for(int i = (x); i < n; i++)
23 #define nfor(i, x, n) for(int i = n-1; i >= x; i--)
24 typedef long long ll;
25 const int inf = 0x3f3f3f3f;
26 const ll INF =0x3f3f3f3f3f3f3f3f;
27 const double pi = acos(-1.0);
28 const double eps = 1e-5;
29 const ll mod = 1e9+7;
30 int a[1010], dp[1010]; 
31 
32 int main() {
33     int n;
34     while(~scanf("%d", &n), n) {
35         forn(i, 0, n) {
36             scanf("%d", &a[i]);
37         }
38         int ans;
39         forn(i, 0, n) {
40             ans = -inf;
41             forn(j, 0, i) {
42                 if(a[j] < a[i])//找最大的dp[j] 
43                     ans = max(dp[j], ans);
44             }
45             dp[i] = max(a[i], ans + a[i]);//dp[i]
46         }
47         ans = -inf;
48         forn(i, 0, n) {
49             ans = max(ans, dp[i]);
50         }
51         printf("%d
", ans);
52     }
53 }

复杂的AC代码(同时记录最长长度):

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <math.h>
 4 #include <string.h>
 5 #include <stdlib.h>
 6 #include <string>
 7 #include <vector>
 8 #include <set>
 9 #include <map>
10 #include <queue>
11 #include <algorithm>
12 #include <sstream>
13 #include <stack>
14 using namespace std;
15 #define mem(a,b) memset((a),(b),sizeof(a))
16 #define mp make_pair
17 #define pb push_back
18 #define fi first
19 #define se second
20 #define sz(x) (int)x.size()
21 #define all(x) x.begin(),x.end()
22 #define forn(i, x, n) for(int i = (x); i < n; i++)
23 #define nfor(i, x, n) for(int i = n-1; i >= x; i--)
24 typedef long long ll;
25 const int inf = 0x3f3f3f3f;
26 const ll INF =0x3f3f3f3f3f3f3f3f;
27 const double pi = acos(-1.0);
28 const double eps = 1e-5;
29 const ll mod = 1e9+7;
30 int a[1010], dp[1010], w[1010];
31 
32 int main() {
33     int n;
34     while(~scanf("%d", &n), n) {
35         forn(i, 0, n) {
36             scanf("%d", &a[i]);
37             w[i] = a[i];
38         }
39         int maxx = -1;
40         forn(i, 0, n) {
41             dp[i] = 1;
42             int temp = w[i];//因为下面会改变w[i] 的 值 
43             forn(j, 0, i) {
44                 if(a[j] < a[i] && dp[j] + 1 > dp[i]) {
45                     dp[i] = dp[j] + 1;//dp存的是最长严格上升序列 
46                     if(temp + w[j] > w[i])
47                         w[i] = temp + w[j];//以i结尾的最大和 
48                 }
49             }
50             maxx = max(w[i], maxx);
51         }
52         printf("%d
", maxx);
53     }
54 }

 

原文地址:https://www.cnblogs.com/ACMerszl/p/9572933.html