原题链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1134
思路:经典的LIS问题,nlogn做法:考虑dp[i]为长度为i的最长上升子序列的末尾元素,那么对于长度为i的最长子序列,如果当前元素为j,并且dp[i]>j,那么我们可以考虑用j替换前面的元素,因为末尾元素越低的话,后面的元素就有更多的机会构造出更长的最长上升子序列,又dp数组是递增的,所以二分查找一下即可。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int MAXN = 5e4 + 5;
const double eps = 1e9;
typedef long long LL;
int main() {
int n, dp[MAXN], t, cnt = 1;
scanf("%d", &n);
scanf("%d", &t);
dp[0] = t;
for (int i = 1; i < n; i++) {
scanf("%d", &t);
if (dp[cnt - 1] < t) dp[cnt++] = t;
else {
int p = lower_bound(dp, dp + cnt, t) - dp;
dp[p] = t;
}
}
printf("%d\n", cnt);
return 0;
}