分析:

完整 代码:

 // 最长不下降子序列
#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. c#画图之折线图

    public JsonResult DrawLineChart() { // 预置颜色 List<Color> colors = new List<Color>() { Col ...

  2. Python 实现选择排序

    选择排序算法步骤: 找到数组中最小的那个元素中, 将它和数组的第一个元素交换位置, 在剩下的元素中找到最小的元素,将它和数组的第二个元素交换位置, 如此往复,知道将整个数组排序. 逐步分析: 假设一个 ...

  3. MySQL优化、锁

    1.  MySQL优化-查看执行记录 MySQL 提供了一个 EXPLAIN 命令, 它可以对 SELECT 语句进行分析, 并输出 SELECT 执行的详细信息, 以供开发人员针对性优化. 使用ex ...

  4. MySQL基础篇(02):从五个维度出发,审视表结构设计

    本文源码:GitHub·点这里 || GitEE·点这里 一.数据场景 1.表结构简介 任何工具类的东西都是为了解决某个场景下的问题,比如Redis缓存系统热点数据,ClickHouse解决海量数据的 ...

  5. 循环删除List集合的元素

    之前在使用list集合循环删除元素的时候,竟然出现了集合内的元素不能删除成功的问题,之后整理了一下,发现大有玄机! 1.如果指定了list的size大小,会出现下标越界异常 List<Strin ...

  6. P4197 Peaks [克鲁斯卡尔重构树 + 主席树][克鲁斯卡尔重构树学习笔记]

    Problem 在\(Bytemountains\)有\(n\)座山峰,每座山峰有他的高度\(h_i\) .有些山峰之间有双向道路相连,共\(M\)条路径,每条路径有一个困难值,这个值越大表示越难走, ...

  7. Fhq Treap [FhqTreap 学习笔记]

    众所周知 Fhq Treap 是 fhq 神仙研究出来的平衡树- 具体实现 每个点实现一个 \(\text{rnd}\) 表示 rand 的值 为什么要 rand 呢 是为了保证树高为 \(\log ...

  8. 永久激活2018.3.5版phpstorm

    下载文件JetbrainsIdesCrack-4.2.jar 文件在后面的附件 配置文件在访达的应用程序中找到phpstorm或者idea,右击,选择显示包含内容,点击显示进入Contents-> ...

  9. salt-minion启动报错No module named salt.scripts

    这是当初部署saltstack时候的问题了,saltstack用的是0.17.4的版本.正当minion部署到最后时候,启动 minion端时报错ImportError: No module name ...

  10. SpringJpa CRUD 代码生成器

    利用业余时间撸了一个Spring Jpa代码生成器jpa-codegen. 简介 这是一款基于Freemarker模板驱动的代码生成器. 依据现有的实体类代码,自动生成CRUD代码,解放双手,加快开发 ...