跳到正文

题解

AT_abc134_e题解

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

文章信息

发布
2023年8月2日
收录
2026年6月26日
更新
2026年11月16日
篇幅
约 1 分钟
获赞
1

版本

  1. v1当前

题目分析

这题不就是求有多少个上升子序列吗。

导弹拦截(大佬请自动忽略)。

这题就是暴力,但是如果从 1−ans1-ans 暴力一遍时间复杂度为 O(n2)O(n^2),会超时,那怎么办呢?有了用二分就能完美避免这个问题,现在时间复杂度为 O(nlog⁡n)O(n \log n),最后输出 ansans 就行啦。

代码!

C++
#include<bits/stdc++.h>
using namespace std;
int n,x,k,ans;
int a[100005];
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>x;
		k=upper_bound(a+1,a+ans+1,x,greater<int>())-a;//二分
		if(k>ans){//比较
			a[++ans]=x;	
		}
		else{
			a[k]=x;
		}
	}
	cout<<ans;//输出
	return 0;//好习惯
}

下期见!