动态规划 ---- 最长不下降子序列(Longest Increasing Sequence, LIS)
分析:
完整 代码:
// 最长不下降子序列
#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 ;
}
题型实战:
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 (≤104) 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)的更多相关文章
- 动态规划——最长不下降子序列(LIS)
最长不降子序列是这样一个问题: 下面介绍动态规划的做法. 令 dp[i] 表示以 A[i] 结尾的最长不下降序列长度.这样对 A[i] 来说就会有两种可能: 如果存在 A[i] 之前的元素 A[j] ...
- 算法实践--最长递增子序列(Longest Increasing Subsquence)
什么是最长递增子序列(Longest Increasing Subsquence) 对于一个序列{3, 2, 6, 4, 5, 1},它包含很多递增子序列{3, 6}, {2,6}, {2, 4, 5 ...
- Luogu 1020 导弹拦截(动态规划,最长不下降子序列,二分,STL运用,贪心,单调队列)
Luogu 1020 导弹拦截(动态规划,最长不下降子序列,二分,STL运用,贪心,单调队列) Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺 ...
- 【动态规划+高精度】mr360-定长不下降子序列
[题目大意] 韵哲君发现自己的面前有一行数字,当她正在琢磨应该干什么的时候,这时候,陈凡老师从天而降,走到了韵哲君的身边,低下头,对她耳语了几句,然后飘然而去. 陈凡老师说了什么呢,陈凡老师对韵哲君说 ...
- HDU 6357.Hills And Valleys-字符串非严格递增子序列(LIS最长非下降子序列)+动态规划(区间翻转l,r找最长非递减子序列),好题哇 (2018 Multi-University Training Contest 5 1008)
6357. Hills And Valleys 自己感觉这是个好题,应该是经典题目,所以半路选手补了这道字符串的动态规划题目. 题意就是给你一个串,翻转任意区间一次,求最长的非下降子序列. 一看题面写 ...
- 【动态规划】【二分】【最长不下降子序列】洛谷 P1020 导弹拦截
最长不下降子序列的nlogn算法 见 http://www.cnblogs.com/mengxm-lincf/archive/2011/07/12/2104745.html 这题是最长不上升子序列,倒 ...
- 算法进阶 (LIS变形) 固定长度截取求最长不下降子序列【动态规划】【树状数组】
先学习下LIS最长上升子序列 看了大佬的文章OTZ:最长上升子序列 (LIS) 详解+例题模板 (全),其中包含普通O(n)算法*和以LIS长度及末尾元素成立数组的普通O(nlogn)算法,当然还 ...
- Educational Codeforces Round 97 (Rated for Div. 2) E. Make It Increasing(最长非下降子序列)
题目链接:https://codeforces.com/contest/1437/problem/E 题意 给出一个大小为 \(n\) 的数组 \(a\) 和一个下标数组 \(b\),每次操作可以选择 ...
- 【C/C++】最长不下降子序列/动态规划
#include <iostream> #include <vector> using namespace std; int main() { //输入 int tmp; ve ...
随机推荐
- Mac 终端 Tomcat 环境配置过程
Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache.Sun 和其他一些公司及个人共同开发而成.Tomc ...
- CentOS7安装Python3.6.8
1.首先通过yum安装python可能用到的依赖 yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel ...
- fatal error LNK1169: one or more multiply defined symbols found
在 Project/Setting/Link/General中的 Project Options: 加入 /FORCE:MULTIPLE即可")可以解决报错问题,但是这些问题全部变成了war ...
- 【MySql】数据库连接异常:Thelastpacketsentsuccessfullytotheserverwas0millisecondsago
参考链接:http://blog.sina.com.cn/s/blog_7540bf5f0102xjpk.html 最近新入职,用了新版的mysql8数据库,结果连接数据库时出现了问题,报了几个异常, ...
- SVN提交更新文件,抛出"svn: No such revision 27106"异常问题处理
SVN,不管是更新或者是提交原来存在的文件,都会抛出此异常"svn: No such revision 27106",注意,是原来存在的文件,要是新增的文件,不会出现此问题. 百度 ...
- c++精度
- Python之pptx实现添加内容与删除(移动)页操作
问题背景 大量表格数据需要生成指定格式的ppt文件,内容以文字和表格为主,首尾页与内容有固定格式.博主不熟悉VBA操作,希望通过模板用Python完成自动化. 基本思路 使用xlrd模块读取xlsx文 ...
- 【spring boot】SpringBoot初学(5)– WebService之Jersey
前言 github: https://github.com/vergilyn/SpringBootDemo 代码位置: 一.准备 spring boot对jersey1.x与jersey2.x的注入方 ...
- Spark学习之路 (九)SparkCore的调优之数据倾斜调优[转]
调优概述 有的时候,我们可能会遇到大数据计算中一个最棘手的问题--数据倾斜,此时Spark作业的性能会比期望差很多.数据倾斜调优,就是使用各种技术方案解决不同类型的数据倾斜问题,以保证Spark作业的 ...
- 敏捷@Scrum基础知识
敏捷,用以概括一套全新的软件开发价值观:敏捷宣言由价值观和原则组成. (一)敏捷核心价值观 敏捷宣言 个体和交互 胜过 过程和工具 可以工作的软件 胜过 面面俱 ...