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

输入

7
1 7 3 5 9 4 8
6
1 3 5 2 4 6

输出

4
4
大佬代码:
 #include <iostream>
using namespace std;
int main(){
    int N;
    while(cin >> N){
        int  a[], dp[] = {}, m=;
    for (int i = ; i <= N; i++) cin >> a[i];
    for (int i = ; i <= N; i++)
        for (int j = ; j < i; j++)
            if (a[j] < a[i])
                dp[i] = max(dp[i], dp[j] + ), m = dp[i] > m ? dp[i] : m;
    cout << m + << endl;
    }
    return ;
}

注意:这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
 #include<iostream>
#include<algorithm>
#include<climits> using namespace std; int main()
{
int t;
while (cin >> t && t != )
{
int a[] = { }, MAX = INT_MIN, ko, b[] = { };
for (int i = ; i < t; i++)
{
cin >> a[i];
}
for (int i = ; i < t; i++)
b[i] = INT_MIN;
b[] = a[];
for (int i = ; i < t; i++)
{
for (int j = ; j < i; j++)
{
if (a[j] < a[i])
b[i] = max(b[i], b[j] + a[i]);
}
b[i] = max(b[i], a[i]);
}
for (int i = ; i < t; i++)
{
if (b[i] > MAX)
MAX = b[i];
}
cout << MAX << endl;
}
return ;
}

特别注意:有些数循环时没有进入第二层的循环,不过也应该放在b数组中进行比较,选取较大的数,相当于最长序列中题中给标记数组初始化为1;

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

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

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 41944   Acc ...

  2. LCS最长公共子序列~dp学习~4

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 Palindrome Time Limit: 4000/2000 MS (Java/Others ...

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

    题意:最长上升子序列nlogn写法 #include<iostream> #include<cstdio> #include<cstring> #include&l ...

  4. POJ 1458 最长公共子序列(dp)

    POJ 1458 最长公共子序列 题目大意:给出两个字符串,求出这样的一 个最长的公共子序列的长度:子序列 中的每个字符都能在两个原串中找到, 而且每个字符的先后顺序和原串中的 先后顺序一致. Sam ...

  5. 【BZOJ2423】[HAOI2010]最长公共子序列 DP

    [BZOJ2423][HAOI2010]最长公共子序列 Description 字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字 ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Jav ...

  7. 最长公共子序列 DP

    class Solution: def LCS(self,A,B): if not A or not B: #边界处理 return 0 dp = [[0 for _ in range(len(B)+ ...

  8. 38-最长公共子序列(dp)

    最长公共子序列 https://www.nowcoder.com/practice/c996bbb77dd447d681ec6907ccfb488a?tpId=49&&tqId=293 ...

  9. 洛谷-P1439 【模板】最长公共子序列 (DP,离散化)

    题意:给两个长度为\(n\)的全排列,求他们的LCS 题解:这题给的数据范围到\(10^5\),用\(O(n^2)\)的LCS模板过不了,但由于给的是两个全排列,他们所含的元素都是一样的,所以,我们以 ...

  10. bzoj3304[Shoi2005]带限制的最长公共子序列 DP

    题意:给出三个序列,求出前两个的公共子序列,且包含第三个序列,要求长度最长. 这道题目怎么做呢,f[i][j]表示a串1-i,b串1-j的最长,g[i][j]表示a串i-n,b串j-m最长, 那么只需 ...

随机推荐

  1. Kaldi的关键词搜索(Keyword Search,KWS)

    本文简单地介绍了KWS的原理--为Lattice中每个词生成索引并进行搜索:介绍了如何处理OOV--替补(Proxy,词典内对OOV的替补)关键词技术:介绍了KWS的语料库格式:介绍了KWS在Kald ...

  2. 前端分辨pc和移动端导入不同css

    通过navigator获取浏览器,根据浏览器的不同判断出pc和移动端然后设置不同的css 分辨不同屏幕导入不同的css文件: function loadCSS() { if((navigator.us ...

  3. 【mmall】mybatis三剑客

    mybatis-generator mybatis-plugin Mybatis Plugin插件安装破解及使用:http://blog.csdn.net/u011410529/article/det ...

  4. Java8新特性(待更新...)

    一.Lambda表达式 二.接口的默认方法与静态方法 三.方法引用 四.重复注解 五.扩展注解的支持 六.Optional 七.Stream 八.Date/Time API (JSR 310) 九.J ...

  5. Python问题:'Nonetype' object is not iterable

    参考链接:http://blog.csdn.net/dataspark/article/details/9953225 [解析] 这个错误提示一般发生在将None赋给多个值时. [案例] 定义了如下的 ...

  6. 用例该如何书写?完整示例-QQ登录界面

    测试用例(case\测试点):指导软件测试工程师找bug的(思想逻辑的整理) 意义:1:怕忘:2:存档(让新人熟悉:产出):3:回归测试(软件即将上线之前,重新执行测试用例)--确认测试 书写测试用例 ...

  7. 二叉查找树(Binary Search Tree)

    二叉树的一个重要的应用是他们在查找中的使用. 以下是二叉查找树的查找代码 #include <stdio.h> int main() { typedef struct Node{ int ...

  8. OGG初始化之将数据从文件加载到Replicat

    要使用Replicat建立目标数据,可以使用初始加载Extract从源表中提取源记录,并将它们以规范格式写入提取文件.从该文件中,初始加载Replicat使用数据库接口加载数据.在加载过程中,更改同步 ...

  9. Fiddler功能介绍

    1.对话框:添加备注,添加完了会在控制面板中的comments显示2.Replay:选中会话后点击,会重新发送请求3.Go:是打断点后,想要继续执行,就点击GO 4.Stream:模式切换. 默认是缓 ...

  10. RocketMQ实战快速入门

    转自:https://www.jianshu.com/p/824066d70da8 一.RocketMQ 是什么      Github 上关于 RocketMQ 的介绍:RcoketMQ 是一款低延 ...