当前位置:网站首页>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
边栏推荐
猜你喜欢
Embedded Software Engineer career planning
线性DP AcWing 902. 最短编辑距离
Is the neural network (pinn) with embedded physical knowledge a pit?
深拷貝 事件總線
Find the common ancestor of any two numbers in a binary tree
Rust search server, rust quick service finding tutorial
JS8day(滚动事件(scroll家族),offset家族,client家族,轮播图案例(待做))
Sweetheart leader: Wang Xinling
China traffic sign detection data set
JS7day(事件对象,事件流,事件捕获和冒泡,阻止事件流动,事件委托,学生信息表案例)
随机推荐
染色法判定二分图 AcWing 860. 染色法判定二分图
Direct control PTZ PTZ PTZ PTZ camera debugging (c)
Drools executes string rules or executes a rule file
Window10 upgrade encountered a big hole error code: 0xc000000e perfect solution
Rust语言文档精简版(上)——cargo、输出、基础语法、数据类型、所有权、结构体、枚举和模式匹配
正确遍历EntryList方法
[ybtoj advanced training guide] similar string [string] [simulation]
Anti shake throttle
Heap acwing 838 Heap sort
FBX import under ue4/ue5 runtime
Lekao: 22 year first-class fire engineer "technical practice" knowledge points
There is a hidden danger in CDH: the exchange memory used by the process of this role is XX megabytes. Warning threshold: 200 bytes
This "little routine" is set on the dough cake of instant noodles. No wonder programmers are always hungry
Bom Dom
Programmers can't find jobs after the age of 35? After reading this article, you may be able to find the answer
应用LNK306GN-TL 转换器、非隔离电源
计数类DP AcWing 900. 整数划分
Drools executes the specified rule
LTC3307AHV 符合EMI标准,降压转换器 QCA7005-AL33 PHY
线性DP AcWing 898. 数字三角形