[算法]LIS && LDS(严格和非严格)

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的排列,所以把第二个串的值改成在第一个串中的位置,然后求最长上升子序列即可

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值