【动态规划】最长上升子序列

最长上升子序列

const int MAXN = 2e5 + 10;
int n, a[MAXN], dp[MAXN];

int LIS1() {
    memset(dp, INF, sizeof(dp[0]) * (n + 1));
    for(int i = 1; i <= n; ++i)
        *lower_bound(dp + 1, dp + 1 + n, a[i]) = a[i];
    int len = 0;
    while(dp[len + 1] != INF)
        ++len;
    return len;
}

最长不下降子序列

int LIS2() {
    memset(dp, INF, sizeof(dp[0]) * (n + 1));
    for(int i = 1; i <= n; ++i)
        *upper_bound(dp + 1, dp + 1 + n, a[i]) = a[i];
    int len = 0;
    while(dp[len + 1] != INF)
        ++len;
    return len;
}

https://www.luogu.com.cn/problem/P1020

导弹拦截

第一问:求最长不上升子序列的长度,第二问:根据Dilworth定理,求最少要多少个不上升子序列覆盖整个序列,就是求最长上升子序列的长度。最长不上升子序列,取相反数之后就变成最长不下降子序列。

for(int i = 1; i <= n; ++i)
    a[i] = -a[i];
pr(LIS2());
for(int i = 1; i <= n; ++i)
    a[i] = -a[i];
pr(LIS1());
原文地址:https://www.cnblogs.com/purinliang/p/14337823.html