算法导论 DP LIS(O(nlog(n))复杂度实现)

关键在于二分查找

/*
    Author:html_11
*/
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <queue>
#include <climits>
#include <set>
#include <stack>
#include <string>
#include <map>
#include <vector>
#define INF 0x3f3f3f3f
#define e 2.718281828459
#define eps 1e-10
using namespace std;
typedef long long ll;
static const int MAX_N = 1e3 + 5;
static const ll Mod = 2009;
int a[MAX_N];
int main() {
	/* freopen("input.txt", "r", stdin);
	 freopen("output.txt", "w", stdout);*/
	int n;
	while (scanf("%d", &n) != EOF) {
		int cnt = 0;
		a[cnt] = -INF;
		for (int i = 0; i < n; ++i) {
			int v;
			scanf("%d", &v);
			if (a[cnt] < v) a[++cnt] = v;
			else {
				int l = 1, r = cnt;
				while (l <= r) {
					int m = (l + r) >> 1;
					if (a[m] >= v) r = m - 1;
					else l = m + 1;
				}
				a[l] = v;
			}
		}
		printf("%d\n", cnt);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值