51 Nod 1134 最长递增子序列 LIS NlogN 写法
题目链接:51Nod 1134 最长递增子序列
思路:相对N^2的DP 算法, NlogN是对内层循环进行优化。代码中国B数组保存的是A[0]~A[i-1]去重过后的有序的序列。然后通过二分查找进行的优化。
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w",stdout)
#define fst first
#define snd second
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
// typedef __int64 LL;
typedef long long LL;
typedef unsigned int uint;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int MAXN = 50000 + 5;
const int MAXM = 10000 + 5;
int N;
int A[MAXN], B[MAXN];
int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif // ONLINE_JUDGE
while (~scanf("%d", &N)) {
for (int i = 0; i < N; i++) {
scanf("%d", &A[i]);
}
int len = 1;
B[0] = A[0];
for (int i = 1; i < N; i++) {
if (A[i] == B[len - 1]) continue;
if (A[i] > B[len - 1]) {
B[len ++] = A[i];
} else {
// >= A[i]
int pos = lower_bound(B, B + len, A[i]) - B;
B[pos] = A[i];
}
}
printf("%d\n", len);
}
return 0;
}