普通
#include<iostream>
using namespace std;
#include<algorithm>
#include<cstring>
const int N = 5010;
int n, f[N];
int a[N];
int main() {
cin >> n;
for (int i = 1; i <= n; i++)cin >> a[i];
for (int i = 1; i <= n; i++)f[i] = 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j < i; j++)
if(a[i]>a[j])f[i] = max(f[i], f[j] + 1);
int res = 0;
for (int i = 1; i <= n; i++)
res = max(res, f[i]);
cout << res;
}
二分优化
#include<iostream>
using namespace std;
const int N = 5010;
int a[N], b[N], f[N];
int n;
int len;
int find(int x) {
int l = 1, r = len, mid;
while (l <= r) {
mid = (l + r) / 2;
if (b[mid] == x)return mid;
else if (x < b[mid])r = mid - 1;
else l = mid + 1;
}
return l;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++)cin >> a[i];
int i, j;
len = 1; b[1] = a[1];
for (int i = 2; i <= n; i++) {
if (a[i] > b[len])
b[++len] = a[i];
else
{
j = find(a[i]);
b[j] = a[i];
}
}
cout << len << endl;
}
标签:5010,include,const,int,len,find,序列,上升,最长
From: https://www.cnblogs.com/windzhao6/p/18347098