当前位置:网站首页>Linear DP acwing 896 Longest ascending subsequence II
Linear DP acwing 896 Longest ascending subsequence II
2022-07-02 12:43:00 【T_ Y_ F666】
linear DP AcWing 896. Longest ascending subsequence II
Original link
AcWing 896. Longest ascending subsequence II
Algorithm tags
greedy
Ideas
For length is 1 Suborder of , Can be stored a[1], It can also be stored a[2], however a[2] Is better than a[1], Because if the subsequent numbers can be compared with a[1] Constitute ascending subsequence , Then it must be possible to a[2] Constitute ascending subsequence , Therefore, the storage length is i The minimum value at the end of the ascending sequence , Using the method of counter evidence, we can get that the ending value rises with the length of the longest rising sequence .
simulation
Time complexity
For ordered arrays , Find the maximum value less than a certain number , You can use dichotomy , Search efficiency log(n), n Queries , The time complexity is n ∗ l o g ( n ) n*log(n) n∗log(n)
Code
#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;
}
Originality is not easy.
Reprint please indicate the source
If it helps you Don't forget to praise and support
边栏推荐
- [ybtoj advanced training guidance] cross the river [BFS]
- 计数类DP AcWing 900. 整数划分
- 应用LNK306GN-TL 转换器、非隔离电源
- Embedded Software Engineer career planning
- Does C language srand need to reseed? Should srand be placed in the loop? Pseudo random function Rand
- 单指令多数据SIMD的SSE/AVX指令集和API
- 区间DP AcWing 282. 石子合并
- Oracle从入门到精通(第4版)
- Efficiency comparison between ArrayList and LinkedList
- Drools executes string rules or executes a rule file
猜你喜欢
1380. Lucky numbers in the matrix [two-dimensional array, matrix]
NTMFS4C05NT1G N-CH 30V 11.9A MOS管,PDF
线性DP AcWing 899. 编辑距离
LTC3307AHV 符合EMI标准,降压转换器 QCA7005-AL33 PHY
Writing method of then part in drools
ArrayList与LinkedList效率的对比
[ybtoj advanced training guidance] cross the river [BFS]
VLAN experiment
Deep copy event bus
Adding database driver to sqoop of cdh6
随机推荐
防抖 节流
js 迭代器 生成器 异步代码处理 promise+生成器 -> await/async
Deep copy event bus
Use MySQL events to regularly perform post seven world line tasks
ArrayList与LinkedList效率的对比
CV2 in OpenCV VideoWriter_ Fourcc() function and cv2 Combined use of videowriter() function
Typora+docsify quick start
Calculate the maximum path sum of binary tree
Direct control PTZ PTZ PTZ PTZ camera debugging (c)
2.6 using recursion and stack - [tower of Hanoi problem]
线性DP AcWing 895. 最长上升子序列
JS7day(事件对象,事件流,事件捕获和冒泡,阻止事件流动,事件委托,学生信息表案例)
AI mid stage technology research
. Net, C # basic knowledge
包管理工具
2.7 binary tree, post order traversal - [FBI tree]
通过反射执行任意类的任意方法
Intel 内部指令 --- AVX和AVX2学习笔记
Adding database driver to sqoop of cdh6
Window10 upgrade encountered a big hole error code: 0xc000000e perfect solution