LIS
#include<bits/stdc++.h>
using namespace std;
int dp[100005];
int main()
{
int n, x;
cin >> n;
memset(dp, 0x3f, sizeof(dp));
for (int i = 0; i < n; ++i){
cin >> x;
*lower_bound(dp, dp + n, x) = x;
}
cout << lower_bound(dp, dp + n, 0x3f3f3f3f) - dp << endl;
return 0;
}
最长先上升后下降子序列
#include<bits/stdc++.h>
#define maxn 105
#define inf 0x3f3f3f3f
using namespace std;
int a[maxn], b[maxn], dp[maxn], dpl[maxn], l[maxn], r[maxn];
int main()
{
int n, ans = 0;
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
for (int i = 0, j = n - 1; i < n; ++i, --j) b[j] = a[i];
memset(dp, 0x3f, sizeof(dp));
memset(dpl, 0x3f, sizeof(dpl));
for (int i = 0; i < n; ++i){
*lower_bound(dp, dp + n, a[i]) = a[i];
l[i] = lower_bound(dp, dp + n, inf) - dp;
*lower_bound(dpl, dpl + n, b[i]) = b[i];
r[i] = lower_bound(dpl, dpl + n, inf) - dpl;
}
for (int i = 0; i < n; ++i){
for (int j = i + 1; j < n; ++j){
ans = max(ans, l[i] + r[n - 1 - j]);
}
}
printf("%d\n", n - ans);
return 0;
}
洛谷 – 1020 – 导弹拦截(最长严格或非严格上升或下降子序列)
https://www.luogu.org/problemnew/show/P1020
最长非降子序列不等于最长上升子序列,最长非降子序列要用upper_bound,最长上升子序列要用lower_bound,被坑爆了。
P1439 【模板】最长公共子序列
https://www.luogu.org/problemnew/show/P1439
因为这道题的两个序列是1到n的排列,所以把第二个串的值改成在第一个串中的位置,然后求最长上升子序列即可