当前位置:网站首页>All in one 1281 Dynamic programming of the longest ascending subsequence solution
All in one 1281 Dynamic programming of the longest ascending subsequence solution
2022-06-10 06:51:00 【51CTO】
Topic link : http://ybt.ssoier.cn:8088/problem_show.php?pid=1281
The main idea of the topic :
I'll give you a sequence \(a_1, a_2, \ldots, a_n\), You need to find some subscripts in the sequence \(i_1, i_2, \ldots, i_k\) At the same time satisfy :
- \(1 \le i_1 \lt i_2 \lt \ldots \lt i_k \le n\);
- \(a_{i_1} \lt a_{i_2} \lt \ldots \lt a_{i_k}\)
namely : Find out a subsequence of the sequence that each element in the subsequence is smaller than the element after it .
The longest subsequence that satisfies this property ( Contains up to ) The subsequence of is called the sequence of numbers “ Longest ascending subsequence ” .
notes :“ Longest ascending subsequence ” English is “Longest increasing subsequence”, abbreviation “LIS”, So we usually put “ Longest ascending subsequence ” The problem is abbreviated as LIS problem .
What this problem solves is the length of the longest ascending subsequence .
Their thinking :
This problem is solved by dynamic programming ( Attention, everyone : And then I'm going to “LIS” To express “ Longest ascending subsequence ” 了 ).
Define the State \(f_i\) Said to \(a_i\) At the end of the LIS length .
Be careful : here “ With \(a_i\) ending ” It means this LIS Must contain \(a_i\)( in other words \(a_i\) yes LIS The last element in )
The first thing you can be sure of is \(f_i\) At least for \(1\)( because \(a_i\) A single number can form a length of \(1\) Of LIS).
Secondly, we can think of the words , For any \(j(1 \le j \lt i)\), if \(a_j \lt a_i\), be \(a_i\) You can follow \(a_j\) Then a ascending subsequence is formed , And then \(a_j\) At the end of the LIS The length is \(f_j\), be \(a_i\) With the \(a_j\) What can be formed later LIS The length is \(f_j + 1\).
Accordingly , We can derive the state transition equation as follows :
\(f_i = \max\limits_{j = 1 \text{ to }i-1 \text{ and } a_j \lt a_i} \{ f_j \} + 1\)
And \(f_i\) At least for \(1\).
Find all the \(f_i(1 \le i \le n)\) after , We can find out , Of this series LIS It must be in some way \(a_i\) At the end of the , So we can get the LIS by \(\max\limits_{i = 1 \text{ to } n}\{ f_i \}\) .
The example program is as follows :
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
int n, a[maxn], f[maxn], ans;
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i ++)
scanf("%d", a+i);
for (int i = 1; i <= n; i ++)
{
f[i] = 1;
for (int j = 1; j < i; j ++)
if (a[j] < a[i])
f[i] = max(f[i], f[j] + 1);
}
for (int i = 1; i <= n; i ++)
ans = max(ans, f[i]);
printf("%d\n", ans);
return 0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
The time complexity of the above code is \(O(n^2)\), And for LIS The problem has a binary optimization \(O(n \cdot log n)\) Algorithm , Interested students can study it by themselves .
边栏推荐
- loj10131. 「一本通 4.4 例 2」暗的连锁 题解 树上差分(边差分)
- 关于我并不是一个灰帽子的声明
- 618. How to prepare for the great promotion
- Leetcode game 79 biweekly - complete all questions
- LabVIEW控制Arduino实现红外测距(进阶篇—6)
- Nextcloud internal server error the server cannot complete your request workaround
- Marketing strategy and supply forecast report of China's rice industry from 2022 to 2027 (New Edition)
- Leetcode675. Cutting trees for golf competition: priority queue + breadth first to find the shortest path
- Why can't lldb print view bounds? - Why can't LLDB print view. bounds?
- Teleyecontroller v8.69 reconfiguration of keyboard recording function release by:yose
猜你喜欢

Wechat team sharing: how the wechat background does not crash under massive concurrent requests

Oriental Star Hotel Management Catering project - query function

How to solve mysql1045 and find the prompt is not an internal command

pyinstaller

go-zero 微服务实战系列(二、服务拆分)

Teleyecontroller v8.69 reconfiguration of keyboard recording function release by:yose

Liuyongzhi: one code communication defect analysis and architecture design scheme - sound network developer entrepreneurship lecture VOL.02

QT upper computer controls ABB in real time through EGM

一举刷新 54 个中文 NLP 任务基准,ERNIE3.0加持下的EasyDL可能是市面上最好用的NLP开发平台...

POC_Jenkins
随机推荐
一本通1281.最长上升子序列 题解 动态规划
ShardingSphere实践(6)——弹性伸缩
Qt--- create dialog 2: quick dialog design
Common string input stream collation (gets (), fgets, getline (), cin get()、cin. getline())
Data migration of OLAP tool Doris or starrocks
Teleyecontroller v8.16 release complete registry function
常用字符串输入流整理(gets()、fgets、getline()、cin.get()、cin.getline())
数列分块解决区间更新+区间最值问题
【动态规划】博弈论:取石子游戏合集
Why can't lldb print view bounds? - Why can't LLDB print view. bounds?
Proteus仿真p时出现Cannot open‘***\LISA5476.SDF’的错误
Cannot open 'appears when Proteus simulates p * * * \lisa5476 Error in SDF '
YTU——C语言习题 折半查找
Yosezang original signature locator signaturetest v6.36 RLS release
Principe de l'algorithme d'extraction de l'ensemble d'éléments fréquents associés à l'alarme dans le cadre de l'exploitation et de l'entretien intelligents
CMD of Jerry's AI protocol_ SET_ BT_ Addr [chapter]
scala fastjson 获取jsonArray中 某个key的最大值
在 Kubernetes 中基于 StatefulSet 部署 MySQL(下)
Statement that I am not a grey hat
Using fieldmask to improve c grpc service performance yyds dry inventory