最长上升子序列-冗余优化版-二分
重点
1. q中存的是当前所有长度的序列的结尾最小的值
2. q一定是单调递增的,反证法
3. dp思想->贪心
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
int a[N];
int q[N];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
int len = 0;
q[0] = -2e9;
for (int i = 0; i < n; i++) {
int l = 0, r = len;
while (l < r) {
int mid = (l + r + 1) >> 1;
if (q[mid] < a[i]) l = mid;
else r = mid - 1;
}
len = max(len, l + 1);
q[l + 1] = a[i];
}
cout << len << endl;
return 0;
}