当前位置:网站首页>线性DP AcWing 896. 最长上升子序列 II
线性DP AcWing 896. 最长上升子序列 II
2022-07-02 09:43:00 【T_Y_F666】
线性DP AcWing 896. 最长上升子序列 II
原题链接
算法标签
贪心
思路

对于长度为1的子序, 可以存储a[1], 也可以存储a[2], 但是a[2]要优于a[1], 因为若后续数字可以与a[1]构成上升子序列, 则一定可以与a[2]构成上升子序列,因此需要存储长度为i的上升序列结尾最小值,采用反证法可得结尾值随最长上升序列长度上升而上升。
模拟

时间复杂度
对于有序数组, 查找小于某数最大值, 可以采用二分法, 查找效率log(n), n次查询,时间复杂度为 n ∗ l o g ( n ) n*log(n) n∗log(n)
代码
#include<bits/stdc++.h>
#define int long long
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>b;--i)
using namespace std;
const int N = 100005, INF = 0x3f3f3f3f;
int a[N], q[N];
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n=read();
rep(i, 0, n){
a[i]=read();
}
int len=0;
rep(i, 0, n){
int l=0, r=len;
while(l<r){
int mid=l+r+1>>1;
if(q[mid]<a[i]){
l=mid;
}else{
r=mid-1;
}
}
len=max(len, r+1);
q[r+1]=a[i];
}
printf("%lld", len);
return 0;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- BOM DOM
- 刷题---二叉树--2
- 排序---
- 怎样写一篇赏心悦目的英文数学论文
- Anti shake throttle
- Full link voltage measurement
- (C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
- [I'm a mound pytorch tutorial] learning notes
- Less than three months after the programmer was hired, the boss wanted to launch the app within one month. If he was dissatisfied, he was dismissed immediately
- The programmer and the female nurse went on a blind date and spent 360. He packed leftovers and was stunned when he received wechat at night
猜你喜欢

计数类DP AcWing 900. 整数划分
![[ybtoj advanced training guidance] cross the river [BFS]](/img/4e/83f14452ea6410768cdd01e725af2e.jpg)
[ybtoj advanced training guidance] cross the river [BFS]

Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?

JSON序列化 与 解析

Brush questions --- binary tree --2

drools动态增加、修改、删除规则

AAAI 2022 | Peking University & Ali Dharma Institute: pruning and compression of pre training language model based on comparative learning

区间DP AcWing 282. 石子合并

The differences and relationships among port, targetport, nodeport and containerport in kubenetes

Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
随机推荐
Sub thread get request
Intel 内部指令 --- AVX和AVX2学习笔记
Lekao.com: experience sharing of junior economists and previous candidates in customs clearance
drools决策表的简单使用
Go学习笔记—基于Go的进程间通信
Distributed machine learning framework and high-dimensional real-time recommendation system
Introduction to CPU instruction set
2.6 using recursion and stack - [tower of Hanoi problem]
Maximum profit of jz63 shares
Deep understanding of P-R curve, ROC and AUC
CDA data analysis -- Introduction and use of aarrr growth model
Sse/avx instruction set and API of SIMD
CDA数据分析——AARRR增长模型的介绍、使用
Post request body content cannot be retrieved repeatedly
kubenetes中port、targetPort、nodePort、containerPort的区别与联系
FBX import under ue4/ue5 runtime
"As a junior college student, I found out how difficult it is to counter attack after graduation."
单指令多数据SIMD的SSE/AVX指令集和API
寻找二叉树中任意两个数的公共祖先
Leetcode - Sword finger offer 51 Reverse pairs in an array