当前位置:网站首页>线性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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Jenkins voucher management
- [FFH] little bear driver calling process (take calling LED light driver as an example)
- drools执行完某个规则后终止别的规则执行
- 寻找二叉树中任意两个数的公共祖先
- Drools executes string rules or executes a rule file
- SparkContext: Error initializing SparkContext解决方法
- CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
- Fluent fluent library encapsulation
- drools执行String规则或执行某个规则文件
- Docker compose configuration mysql, redis, mongodb
猜你喜欢
drools中then部分的写法
[old horse of industrial control] detailed explanation of Siemens PLC TCP protocol
ThreadLocal的简单理解
Tas (file d'attente prioritaire)
Adding database driver to sqoop of cdh6
Drools dynamically add, modify, and delete rules
Simple understanding of ThreadLocal
Multiply LCA (nearest common ancestor)
【工控老马】西门子PLC Siemens PLC TCP协议详解
The blink code based on Arduino and esp8266 runs successfully (including error analysis)
随机推荐
Openssh remote enumeration username vulnerability (cve-2018-15473)
CDA数据分析——AARRR增长模型的介绍、使用
Find the common ancestor of any two numbers in a binary tree
Fluent fluent library encapsulation
分布式机器学习框架与高维实时推荐系统
AI mid stage technology research
LeetCode—剑指 Offer 37、38
FBX import under ue4/ue5 runtime
H5 to app
Addition, deletion, modification and query of MySQL table (Advanced)
Leetcode922 sort array by parity II
Deep Copy Event bus
Jenkins user rights management
IPhone 6 plus is listed in Apple's "retro products" list
WSL 2 will not be installed yet? It's enough to read this article
深拷貝 事件總線
High performance erasure code coding
The differences and relationships among port, targetport, nodeport and containerport in kubenetes
LeetCode—<动态规划专项>剑指 Offer 19、49、60
包管理工具