506 最长上升子序列II

// 506 最长上升子序列II.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
/*
http://oj.daimayuan.top/course/22/problem/647


给定一个长度为 n的数组 a1,a2,…,an
,问其中的最长上升子序列的长度。也就是说,我们要找到最大的 m 以及数组 p1,p2,…,pm,满足 1≤p1<p2<⋯<pm≤n
 并且 ap1<ap2<⋯<apm。

输入格式
第一行一个数字 n。

接下来一行 n个整数 a1,a2,…,an。

输出格式
一个数,表示答案。

样例输入
6
3 7 4 2 6 8
样例输出
4
数据规模
所有数据保证 1≤n≤100000,1≤ai≤109。
*/

#include <iostream>
#include <cstring>

using namespace std;


const int N = 100010;
int a[N];
long long dp[N];
int n;


int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	memset(dp, 0x3f, sizeof dp);
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		int v = a[i];
		int l = 1; int r = n;
		while (l < r) {
			int mid = l + r >> 1;
			if (dp[mid] >= v) 
				r = mid; 
			else 
				l = mid + 1;
		}
		dp[l] = v;
		ans = max(ans, l);
	}

	cout << ans << endl;


	return 0;
}

 

posted on 2024-12-26 11:41  itdef  阅读(6)  评论(0)    收藏  举报

导航