当前位置:网站首页>线性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;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- 2.6 using recursion and stack - [tower of Hanoi problem]
- 初始JDBC 编程
- 高性能纠删码编码
- 趣味 面试题
- Go学习笔记—基于Go的进程间通信
- Gaode map test case
- arcgis js 4.x 地图中加入图片
- Leetcode - < dynamic planning special> Jianzhi offer 19, 49, 60
- 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
- Is the neural network (pinn) with embedded physical knowledge a pit?
猜你喜欢

PR 2021 quick start tutorial, learn about the and functions of the timeline panel

JSON序列化 与 解析

计数类DP AcWing 900. 整数划分

drools中then部分的写法

Multiply LCA (nearest common ancestor)

Lekao: 22 year first-class fire engineer "technical practice" knowledge points

Jenkins voucher management

BOM DOM

Record the range of data that MySQL update will lock

模块化 CommonJS ES Module
随机推荐
The blink code based on Arduino and esp8266 runs successfully (including error analysis)
Sort---
Bom Dom
Why do programmers have the idea that code can run without moving? Is it poisonous? Or what?
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
The second composition template of postgraduate entrance examination English / chart composition, English chart composition is enough
drools中then部分的写法
2.7 binary tree, post order traversal - [FBI tree]
考研英语二大作文模板/图表作文,英语图表作文这一篇就够了
Map and set
堆(优先级队列)
AI mid stage technology research
Discrimination of the interval of dichotomy question brushing record (Luogu question sheet)
Docker compose configuration mysql, redis, mongodb
计算二叉树的最大路径和
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
分布式机器学习框架与高维实时推荐系统