跳到正文

算法·理论

LIS DP

BestFriend约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年8月18日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 1 分钟
获赞
1

版本

  1. v1当前

二分求最长上升子序列长度以及最长上升子序列的序列

C++
#include<queue>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=1e5+7;
int n,idx;
int s[N],res[N];
signed main(){
	scanf("%d",&n);
	for(int i=1,x;i<=n;i++){
		scanf("%d",&x);
		if(s[idx]<x){
			s[++idx]=x;
		}
		else{
			int p=lower_bound(s+1,s+idx+1,x)-s;
			s[p]=x;
		}
	}
	printf("%d",idx);
	return 0;
}

最长不上升子序列

C++
for(int i=1;i<=n;i++){
  if(!idx||q[idx]>=s[i]){
    q[++idx]=s[i];
  }
	else{
		int p=upper_bound(q+1,q+idx+1,s[i],greater<int>())-q;
		q[p]=s[i];
	}
}
原文