当前位置:网站首页>线性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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- arcgis js 4.x 地图中加入图片
- Tas (file d'attente prioritaire)
- mysql数据库基础
- 深拷贝 事件总线
- Differences between nodes and sharding in ES cluster
- Sort---
- 使用Sqoop把ADS层数据导出到MySQL
- [C language] Yang Hui triangle, customize the number of lines of the triangle
- Multiply LCA (nearest common ancestor)
- When uploading a file, the server reports an error: iofileuploadexception: processing of multipart / form data request failed There is no space on the device
猜你喜欢

使用Sqoop把ADS层数据导出到MySQL

深拷貝 事件總線

Multiply LCA (nearest common ancestor)

Enhance network security of kubernetes with cilium

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

High performance erasure code coding

BOM DOM

CDH6之Sqoop添加数据库驱动

WSL 2 will not be installed yet? It's enough to read this article

深拷贝 事件总线
随机推荐
Intel 内部指令 --- AVX和AVX2学习笔记
SparkContext: Error initializing SparkContext解决方法
使用Sqoop把ADS层数据导出到MySQL
Find the factorial of a positive integer within 16, that is, the class of n (0= < n < =16). Enter 1111 to exit.
趣味 面试题
Go学习笔记—多线程
js 迭代器 生成器 异步代码处理 promise+生成器 -> await/async
CDA数据分析——Excel数据处理的常见知识点归纳
FBX import under ue4/ue5 runtime
[ybtoj advanced training guidance] cross the river [BFS]
Anti shake throttle
drools执行完某个规则后终止别的规则执行
【工控老马】西门子PLC Siemens PLC TCP协议详解
防抖 节流
寻找二叉树中任意两个数的公共祖先
drools执行指定的规则
[C language] Yang Hui triangle, customize the number of lines of the triangle
CDA data analysis -- Introduction and use of aarrr growth model
AAAI 2022 | Peking University & Ali Dharma Institute: pruning and compression of pre training language model based on comparative learning
Interview with meituan, a 34 year old programmer, was rejected: only those under the age of 30 who work hard and earn little overtime