当前位置:网站首页>线性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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
猜你喜欢

Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students
![[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol](/img/13/9002244555ebe8a61660c2506993fa.png)
[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol

MySQL and PostgreSQL methods to grab slow SQL

BOM DOM

drools中then部分的写法

Enhance network security of kubernetes with cilium

排序---

模块化 CommonJS ES Module

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

Performance tuning project case
随机推荐
Shuttle encapsulated AppBar
Post request body content cannot be retrieved repeatedly
How to write a pleasing English mathematical paper
Mysql database foundation
Fluent fluent library encapsulation
防抖 节流
Anxiety of a 211 programmer: working for 3 years with a monthly salary of less than 30000, worried about being replaced by fresh students
WSL 2 will not be installed yet? It's enough to read this article
Go学习笔记—基于Go的进程间通信
浏览器node事件循环
Heap (priority queue)
[C language] convert decimal numbers to binary numbers
Simple understanding of ThreadLocal
2.7 binary tree, post order traversal - [FBI tree]
AI mid stage technology research
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
CV2 in OpenCV VideoWriter_ Fourcc() function and cv2 Combined use of videowriter() function
Docker-compose配置Mysql,Redis,MongoDB
Intel 内部指令 --- AVX和AVX2学习笔记
Sse/avx instruction set and API of SIMD