#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int n, dp[100005], mx, ans;
int *g;
struct Number {
int p1, p2;
} a[100005];
bool cmp(Number a, Number b) {
return a.p1 < b.p1;
}
int main() {
scanf("%d", &n);
for(int i = 0; i < n; ++i)
scanf("%d", &a[i].p1);
for(int i = 0; i < n; ++i)
scanf("%d", &a[i].p2);
sort(a, a + n, cmp);
memset(dp, 0x3f, sizeof(dp));
for(int i = 0; i < n; ++i) {
g = upper_bound(dp, dp + ans, a[i].p2);
*g = a[i].p2;
if(g - dp == ans) ++ans;
}
printf("%d", ans);
return 0;
}
我把LCS转换成求最长上升子序列问题,用了upper_bound 这个 STL 函数,样例输出正确,实现最长上升子序列问题也过了模板题(B3637)。一定要手写吗?