链接:https://www.nowcoder.com/questionTerminal/d83721575bd4418eae76c916483493de
来源:牛客网
广场上站着一支队伍,她们是来自全国各地的扭秧歌代表队,现在有她们的身高数据,请你帮忙找出身高依次递增的子序列。 例如队伍的身高数据是(1、7、3、5、9、4、8),其中依次递增的子序列有(1、7),(1、3、5、9),(1、3、4、8)等,其中最长的长度为4。
输入描述:
输入包含多组数据,每组数据第一行包含一个正整数n(1≤n≤1000)。 紧接着第二行包含n个正整数m(1≤n≤10000),代表队伍中每位队员的身高。
输出描述:
对应每一组数据,输出最长递增子序列的长度。
示例1
输入
71 7 3 5 9 4 861 3 5 2 4 6
输出
44大佬代码:
1 #include <iostream> 2 using namespace std; 3 int main(){ 4 int N; 5 while(cin >> N){ 6 int a[10002], dp[10002] = {0}, m=0; 7 for (int i = 1; i <= N; i++) cin >> a[i]; 8 for (int i = 1; i <= N; i++) 9 for (int j = 1; j < i; j++) 10 if (a[j] < a[i]) 11 dp[i] = max(dp[i], dp[j] + 1), m = dp[i] > m ? dp[i] : m; 12 cout << m + 1 << endl; 13 } 14 return 0; 15 }
注意:这dp题不是很难,但在掌握后必须时刻记住有些题虽然通过这题改编,不过坑很多;
比如:
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.
InputInput 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.
OutputFor 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
1 #include<iostream> 2 #include<algorithm> 3 #include<climits> 4 5 using namespace std; 6 7 int main() 8 { 9 int t; 10 while (cin >> t && t != 0) 11 { 12 int a[1005] = { 0 }, MAX = INT_MIN, ko, b[1005] = { 0 }; 13 for (int i = 0; i < t; i++) 14 { 15 cin >> a[i]; 16 } 17 for (int i = 0; i < t; i++) 18 b[i] = INT_MIN; 19 b[0] = a[0]; 20 for (int i = 1; i < t; i++) 21 { 22 for (int j = 0; j < i; j++) 23 { 24 if (a[j] < a[i]) 25 b[i] = max(b[i], b[j] + a[i]); 26 } 27 b[i] = max(b[i], a[i]); 28 } 29 for (int i = 0; i < t; i++) 30 { 31 if (b[i] > MAX) 32 MAX = b[i]; 33 } 34 cout << MAX << endl; 35 } 36 return 0; 37 }
特别注意:有些数循环时没有进入第二层的循环,不过也应该放在b数组中进行比较,选取较大的数,相当于最长序列中题中给标记数组初始化为1;
原文地址:https://www.cnblogs.com/kangdong/p/8455416.html