分析:

完整 代码:

 // 最长不下降子序列
#include <stdio.h>
#include <algorithm>
using namespace std; const int N = ;
int A[N], dp[N]; int main()
{
freopen("in.txt", "r", stdin);
int n;
scanf("%d", &n);
for (int i = ; i <= n; i++){
scanf("%d", &A[i]);
} int ans = -; // 记录最长的dp[i]
for (int i = ; i <= n; i++){ // 按顺序计算出dp[i]的值
dp[i] = ; // 边界初始条件(先假设每个元素自成一个子序列)
// 如果A[i] >= A[j] 且 A[i]的加入能使dp[i]变长,即dp[j] + 1 > dp[i]
for (int j = ; j < i; j++){
if (A[i] >= A[j] && (dp[j] + > dp[i])){
dp[i] = dp[j] + ; // 状态转移方程,用以更新dp[i]
}
}
ans = max(ans, dp[i]);
} printf("%d", ans);
fclose(stdin); return ;
}

题型实战:

            1045 Favorite Color Stripe(30分)

Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining parts together to form her favorite color stripe.

It is said that a normal human eye can distinguish about less than 200 different colors, so Eva's favorite colors are limited. However the original stripe could be very long, and Eva would like to have the remaining favorite stripe with the maximum length. So she needs your help to find her the best result.

Note that the solution might not be unique, but you only have to tell her the maximum length. For example, given a stripe of colors {2 2 4 1 5 5 6 3 1 1 5 6}. If Eva's favorite colors are given in her favorite order as {2 3 1 5 6}, then she has 4 possible best solutions {2 2 1 1 1 5 6}, {2 2 1 5 5 5 6}, {2 2 1 5 5 6 6}, and {2 2 3 1 1 5 6}.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤200) which is the total number of colors involved (and hence the colors are numbered from 1 to N). Then the next line starts with a positive integer M (≤200) followed by M Eva's favorite color numbers given in her favorite order. Finally the third line starts with a positive integer L (≤10​4​​) which is the length of the given stripe, followed by L colors on the stripe. All the numbers in a line a separated by a space.

Output Specification:

For each test case, simply print in a line the maximum length of Eva's favorite stripe.

Sample Input:

6
5 2 3 1 5 6
12 2 2 4 1 5 5 6 3 1 1 5 6

Sample Output:

7

分析:将喜欢的颜色映射到一个非递减序列,然后将所有输入的颜色且是喜欢的颜色映射到一个数组中,求这个数组中最长的非递减序列长度

代码:

 #include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std; const int maxc = ; // 最大颜色数
const int maxn = ; // 最大的L //
int HashTable[maxc]; // 将喜欢的颜色映射为递增序列,不喜欢的颜色映射为-1
int A[maxn], dp[maxn]; // 最长不下降子序列的原数组A和DP数组 int main()
{
int n, m, x;
scanf("%d%d", &n, &m);
memset(HashTable, -, sizeof(HashTable)); // 将整数HashTable数组初始化为-1
for (int i = ; i < m; i++){
scanf("%d", &x);
HashTable[x] = i;
} int L, num = ; // num存放L个颜色中包含喜欢的颜色的数量
scanf("%d", &L);
for (int i = ; i < L; i++){
scanf("%d", &x);
if (HashTable[x] != -){
A[num++] = HashTable[x];
}
} // 以下为LIS问题的模板
int ans = -;
for (int i = ; i < num; i++){
dp[i] = ;
for (int j = ; j < i; j++){
if (A[j] <= A[i] && dp[j] + > dp[i]){
dp[i] = dp[j] + ;
}
}
ans = max(ans, dp[i]);
} printf("%d\n", ans);
return ;
}

动态规划 ---- 最长不下降子序列(Longest Increasing Sequence, LIS)的更多相关文章

  1. 动态规划——最长不下降子序列(LIS)

    最长不降子序列是这样一个问题: 下面介绍动态规划的做法. 令 dp[i] 表示以 A[i] 结尾的最长不下降序列长度.这样对 A[i] 来说就会有两种可能: 如果存在 A[i] 之前的元素 A[j] ...

  2. 算法实践--最长递增子序列(Longest Increasing Subsquence)

    什么是最长递增子序列(Longest Increasing Subsquence) 对于一个序列{3, 2, 6, 4, 5, 1},它包含很多递增子序列{3, 6}, {2,6}, {2, 4, 5 ...

  3. Luogu 1020 导弹拦截(动态规划,最长不下降子序列,二分,STL运用,贪心,单调队列)

    Luogu 1020 导弹拦截(动态规划,最长不下降子序列,二分,STL运用,贪心,单调队列) Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺 ...

  4. 【动态规划+高精度】mr360-定长不下降子序列

    [题目大意] 韵哲君发现自己的面前有一行数字,当她正在琢磨应该干什么的时候,这时候,陈凡老师从天而降,走到了韵哲君的身边,低下头,对她耳语了几句,然后飘然而去. 陈凡老师说了什么呢,陈凡老师对韵哲君说 ...

  5. HDU 6357.Hills And Valleys-字符串非严格递增子序列(LIS最长非下降子序列)+动态规划(区间翻转l,r找最长非递减子序列),好题哇 (2018 Multi-University Training Contest 5 1008)

    6357. Hills And Valleys 自己感觉这是个好题,应该是经典题目,所以半路选手补了这道字符串的动态规划题目. 题意就是给你一个串,翻转任意区间一次,求最长的非下降子序列. 一看题面写 ...

  6. 【动态规划】【二分】【最长不下降子序列】洛谷 P1020 导弹拦截

    最长不下降子序列的nlogn算法 见 http://www.cnblogs.com/mengxm-lincf/archive/2011/07/12/2104745.html 这题是最长不上升子序列,倒 ...

  7. 算法进阶 (LIS变形) 固定长度截取求最长不下降子序列【动态规划】【树状数组】

    先学习下LIS最长上升子序列 ​ 看了大佬的文章OTZ:最长上升子序列 (LIS) 详解+例题模板 (全),其中包含普通O(n)算法*和以LIS长度及末尾元素成立数组的普通O(nlogn)算法,当然还 ...

  8. Educational Codeforces Round 97 (Rated for Div. 2) E. Make It Increasing(最长非下降子序列)

    题目链接:https://codeforces.com/contest/1437/problem/E 题意 给出一个大小为 \(n\) 的数组 \(a\) 和一个下标数组 \(b\),每次操作可以选择 ...

  9. 【C/C++】最长不下降子序列/动态规划

    #include <iostream> #include <vector> using namespace std; int main() { //输入 int tmp; ve ...

随机推荐

  1. POJ 2556 (判断线段相交 + 最短路)

    题目: 传送门 题意:在一个左小角坐标为(0, 0),右上角坐标为(10, 10)的房间里,有 n 堵墙,每堵墙都有两个门.每堵墙的输入方式为 x, y1, y2, y3, y4,x 是墙的横坐标,第 ...

  2. PMP--1.1 PMBOK体系框架描述

    PMBOK:Project Management Body Of Knowledge(项目管理知识体系) <PMBOK>中收录被普遍认可的良好实践. 普遍认可:在大部分时候适用于大多数项目 ...

  3. Bash脚本编程学习笔记05:用户交互与脚本调试

    用户交互 在<学习笔记04>中我们有提到位置参数,位置参数是用来向脚本传递参数的一种方式.还有一种方式,是read命令. [root@c7-server ~]# read name alo ...

  4. linux命令绕过

    前言: 做ctf时常常会遇到一些正则匹配将一些linux命令给过滤掉,这里将总结一些针对性的绕过方式. 一.空格绕过: {cat,flag.txt} cat${IFS}flag.txt cat$IFS ...

  5. 【剑指Offer】05、用两个栈实现队列

    题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 题解一: //stack2有元素就pop,没有元素就将stack1中所有元素倒进来再pop public ...

  6. 吴裕雄--天生自然HADOOP操作实验学习笔记:hdfs简单的shell命令

    实验目的 了解bin/hadoop脚本的原理 学会使用fs shell脚本进行基本操作 学习使用hadoop shell进行简单的统计计算 实验原理 1.hadoop的shell脚本 当hadoop集 ...

  7. Perl-统计文本中各个单词出现的次数(NVDIA2019笔试)

    1.原题 2.perl脚本 print "================ Method 1=====================\n"; open IN,'<','an ...

  8. redis的一些常见面试题

    1 .在项目中缓存是如何使用的? 2.为啥在项目里要用缓存呢? 用缓存,主要用途,高性能和高并发 高性能: 场景举例:假如有这么一个操作,用户发起请求,操作数据库,查出结果,耗时600ms,然后这个结 ...

  9. 使用touch操作图片

    功能: 1.图片放大缩小 2.图片移动 3.图片上做点标记 PS 1.后端程序员不容易,且行且珍惜 2.代码不想封装,累 js代码 /**************************calc pi ...

  10. Bubble Sort HDU - 5775 树状数组

    //每个数字只会被它后面的比它小的数字影响,且会向右移动相应个数的位置 //比如:6 4 3 5 2 1 .4后面比它小的有 三个,因此它的最右边位置就是当前位置 +3,即5 //如果该数字本身在标准 ...