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;
}
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力

