最长上升子序列(dp)

链接: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

时间: 2024-11-08 20:54:36

最长上升子序列(dp)的相关文章

lis(最长上升子序列) dp

lis(最长上升子序列) dp 求序列的lis,子序列可不连续 for(int i=1;i<=N;i++){ scanf("%d",&a[i]); dp[i]=1; } for(int i=2;i<=N;i++){ for(int j=1;j<i;j++){ if(a[j]<a[i]) dp[i]=max(dp[i],dp[j]+1); } } int ans=1; for(int i=1;i<=N;i++){ //注意并不是dp[N]最大,而是要

POJ-2533最长上升子序列(DP+二分)(优化版)

Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 41944   Accepted: 18453 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ...

hdu 1159 Common Subsequence(最长公共子序列 DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25416    Accepted Submission(s): 11276 Problem Description A subsequence of

OpenJudge Bailian 2757 最长上升子序列 DP

最长上升子序列 Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u OpenJ_Bailian 2757 Description 一个数的序列 bi,当 b1 < b2 < ... < bS的时候,我们称这个序列是上升的.对于给定的一个序列( a1, a2, ..., aN),我们可以得到一些上升的子序列( ai1, ai2, ..., aiK),这里1 <= i1 <

hdu 1159 common sequence (最长公共子序列 dp)

http://acm.hdu.edu.cn/showproblem.php?pid=1159 题意 : 给出两个字符串 求出最长公共子序列 思路: if(str1[i]==str2[j]) { dp[i][j]=max(dp[i-1][j-1]+1,max(dp[i-1][j],dp[i][j-1])); } else dp[i][j]=max(dp[i-1][j],dp[i][j-1]); #include<cstdio> #include<cstring> #include&l

lcs(最长公共子序列),dp

lcs(最长公共子序列) 求两个序列的lcs的长度,子序列可不连续 dp[i][j]=dp[i-1][j-1]+1(a[i]==b[i]) dp[i][j]=max(dp[i-1][j],dp[i][j-1])(a[i]!=b[i]) memset(dp,0,sizeof(dp)); for(int i=1;i<=n1;i++){ for(int j=1;j<=n2;j++){ if(a[i]==b[j]) dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=max(

最长公共子序列 DP

算法老师的作业,一道dp基础题,给你两个序列,问你最长公共子序列是什么,比如:(a,b)是(a,c,d,b)的子序列.注意不是最长公共子串,这里的子序列可以不连续. 两个for循环就出来了,每一个dp[i][j]可以从dp[i-1][j-1].dp[i-1][j].dp[i][j-1]三种情况更新过来,取个最大的,然后把路径用123存下来,最后再顺着路径找然后逆序输出就行. sample input: 7 4 A B C B D A B B C D B 7 6 A B C B D A B B D

Longest Ordered Subsequence POJ - 2533 最长上升子序列dp

题意:最长上升子序列nlogn写法 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int dp[1005]; 7 int a[1005]; 8 int main(){ 9 int n; 10 while(cin>>n){ 11 for(int i=0;i<n;i++){ 12

Bridging signals POJ 1631(最长递增子序列dp)

原题 题目链接 题目分析 由题目知,如果能求出连接点的最长递增子序列,则可以把连接不在该序列中的点的线全部剪掉.而维护最长递增子序列可以用dp来做,考虑到相同长度的递增子序列末尾数字越小越好,可以这样定义dp,dp[i]长度为i的递增子序列的最小末尾值,初始化为INF,由于这个dp具有有序性,因此可以用二分来加快更新,每次遍历到值num[i],只需二分找出大于等于num[i]的更新之即可.最后从扫一遍dp数组即可得到最长长度. 代码 1 #include <iostream> 2 #inclu

最长共同子序列 --- DP(动态规划)

题目,就是首先输入两个串的长度, 接着输入两个串  n = 4 m = 4 s = "abcd" t = "bcde" 输出: 3 ("bcd") 就是公共的最长子序列. 解题分析: 对于这种题目,首先要推倒转移方程,那么这里可以先定义二维数组dp[ i ] [ j ] 然后根据串 s  和 t 的长度 i , j来定义方程 s1....si 与 T1,,....Tj 的共同子序列是dp[ i ] [ j ] 这样一来, S1.....Si 与