动态规划 ---- 最长不下降子序列(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 ...
随机推荐
- cf912D
题意简述:往n*m的网格中放k条鱼,一个网格最多放一条鱼,然后用一个r*r的网随机去捞鱼,问怎么怎么放鱼能使得捞鱼的期望最大,输出这个期望 题解:肯定优先往中间放,这里k不大,因此有别的简单方法,否则 ...
- Win10查看屏保的所在位置
路径 C:\Users\Hlzy\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\Local ...
- P2919 [USACO08NOV]守护农场Guarding the Farm
链接:P2919 ----------------------------------- 一道非常暴力的搜索题 ----------------------------------- 注意的是,我们要 ...
- 【Newtonsoft.Json】json序列化小驼峰格式(属性名首字母小写)
我是一名 ASP.NET 程序员,专注于 B/S 项目开发.累计文章阅读量超过一千万,我的博客主页地址:https://www.itsvse.com/blog_xzz.html 只需要设置JsonSe ...
- Java各种类
1.Object类 equals方法 2.Date类 构造方法 成员方法 DateFormat类 Calendar类 3.System类 StringBuilder原理 构造方法 toString方法 ...
- MVC开发之注入容器Ninject的使用
背景 在不使用注入容器之前,我们的项目往往存在着大量耦合的类,这使得我们在开发大型项目时难以维护.比如下面这个简单的MVC框架的例子,计算购物车的产品价格总和: /// <summary> ...
- Java基础汇总2019
1.事务的ACID性: (1)原子性:要么做,要么都不做.程序操作执行未成功,则所做的更改会被撤销: (2)一致性:比如转账,a转给b一百元,则a的账户少100,b的账户多100,前后数据要一致: ( ...
- PAT (Basic Level) Practice (中文)1016 部分A+B (15 分)
正整数 A 的“DA(为 1 位整数)部分”定义为由 A 中所有 DA 组成的新整数 PA.例如:给定 8,DA=6,则 A 的“6 部分”PA 是 66,因为 A 中有 ...
- jQuery---城市选择案例
城市选择案例 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UT ...
- 另外一种获取redis cluster主从关系和slot分布的方法
条条大路通罗马,通过最近学习redis cluster 观察其输出,发现了另外一种获取master-slave关系的方法. [redis@lxd-vm1 ~]$ cat get_master_slav ...