子序列dp

· · 个人记录

咯咯咯

总结咯咯咯

Orac and Models

简要题意

给定长度为n的序列a,求满足相邻两项在原序列中的下标成倍数关系的最长上升子序列。

思路

设dp[i]表示以i结尾的最长上升子序列长度。

初始dp[i]=1

对于每个位置i,枚举它的所有倍数 2\times i,3\times i,…\le n,如果a[j]>a[i],则dp[j]=dp[i]+1

最终答案为所有dp[i]的最大值。

100分


#include<bits/stdc++.h>
using namespace std;

const int N = 100005; int a[N], dp[N];

void solve() { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i];

int ans = 1;
for (int i = 1; i <= n; i++) dp[i] = 1;

for (int i = 1; i <= n; i++) {
    for (int j = 2 * i; j <= n; j += i) {
        if (a[j] > a[i]) {
            dp[j] = max(dp[j], dp[i] + 1);
        }
    }
    ans = max(ans, dp[i]);
}

cout << ans << '\n';

}

int main() { ios::sync_with_stdio(0); cin.tie(0);

int T;
cin >> T;
while (T--) {
    solve();
}
return 0;

}

# Useless for LIS
## 简要题意
给定长度为n的序列a,输出所有出现在最长上升子序列中的元素下标。
## 思路
>A_i能出现在最长上升子序列中,说明它既能作为结尾的一部分也能作为开头的一部分。
>
>设dp1[i]为以$A_i$结尾 的最长上升子序列长度,dp2[i]为以$A_i$开头的最长上升子序列长度。
>
>若dp1[i]+dp2[i]−1=最长上升子序列长度,则$A_i$一定在最长上升子序列中。
## 100分
```cpp
#include <bits/stdc++.h>
using namespace std;
#define int long long

const int N = 200005;

int a[N], dp1[N], dp2[N];
int f[N];

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int T;
    cin >> T;
    while (T--) {
        int n;
        cin >> n;
        for (int i = 1; i <= n; i++) cin >> a[i] , f[i] = 1e9;
        int len = 0;
        for (int i = 1; i <= n; i++) {
            int pos = lower_bound(f + 1, f + n + 1, a[i]) - f;
            f[pos] = min(f[pos] ,a[i]);
            dp1[i] = pos;
            len = max(len , pos);
        }

        for (int i = 1; i <= n; i++) f[i] = 0;

        for (int i = n; i >= 1; i--) {
            int pos = lower_bound(f + 1, f + n + 1, a[i] , greater<int>()) - f;
            f[pos] = max(f[pos] ,a[i]);
            dp2[i] = pos;
        }
        vector<int> ans;
        for (int i = 1; i <= n; i++) {
            if (dp1[i] + dp2[i] - 1 == len) {
                ans.push_back(i);
            }
        }
        cout << ans.size() << "\n";
        for (auto x : ans) {
            cout << x << " ";
        }
        cout << "\n";
    }
    return 0;
}

合唱队形

简要题意

给定n个人的身高,求得到先递增后递减的子序列最少删除的人数。

思路

求最少删除人数就相当于求最大保留人数,对于每个人i,如果把他放在中间,那么他左边是最长上升子序列,右边是最长下降子序列。

设dp1[i]为以第i个人结尾的最长上升子序列长度。dp2[i]为以第i个人结尾的最长下降子序列长度。

则包含第i个人的最长子序列长度为dp1[i]+dp2[i]−1

最少删除人数即为n-最长子序列长度

100分


#include<bits/stdc++.h>
using namespace std;

const int N = 105; int a[N], dp1[N], dp2[N];

int main() { ios::sync_with_stdio(0); cin.tie(0);

int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];

for (int i = 1; i <= n; i++) {
    dp1[i] = 1;
    for (int j = 1; j < i; j++) {
        if (a[j] < a[i]) dp1[i] = max(dp1[i], dp1[j] + 1);
    }
}

for (int i = n; i >= 1; i--) {
    dp2[i] = 1;
    for (int j = n; j > i; j--) {
        if (a[j] < a[i]) dp2[i] = max(dp2[i], dp2[j] + 1);
    }
}

int len = 0;
for (int i = 1; i <= n; i++) {
    len = max(len, dp1[i] + dp2[i] - 1);
}

cout << n - len << '\n';
return 0;

}

# Good Sequences
## 简要题意
给定严格递增数组$a_1,a_2,…,a_n$,求最长的相邻两项的不互质子序列
## 思路
>由于原数组有序,我们只需选择元素。
>
>设dp[p]表示以含有质因子p的数结尾的最长子序列长度。
>
>对于当前数x,先分解质因子,它能接在任何一个包含这些质因子的数之后,所以当前最长长度为dp[p]+1
>
>最后全局答案就是dp[i]的最大值
## 100分
```cpp
#include <bits/stdc++.h>
using namespace std;
#define int long long

const int MAXA = 100005;

int prim[MAXA];
int dp[MAXA];
int a[MAXA];

void init() {
    for (int i = 2; i < MAXA; i++) prim[i] = i;
    for (int i = 2; i * i < MAXA; i++) {
        if (prim[i] == i) {
            for (int j = i * i; j < MAXA; j += i) {
                if (prim[j] == j) prim[j] = i;
            }
        }
    }
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    init();

    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];

    int ans = 0;
    for (int i = 1; i <= n; i++) {
        int x = a[i];
        if (x == 1) {
            ans = max(ans, 1LL);
            continue;
        }

        int cur = 0;
        vector<int> v;
        int tmp = x;
        while (tmp > 1) {
            int p = prim[tmp];
            v.push_back(p);
            cur = max(cur, dp[p]);
            while (tmp % p == 0) tmp /= p;
        }

        cur++;
        for (auto x:v) {
            dp[x] = max(dp[x], cur);
        }
        ans = max(ans, cur);
    }

    cout << ans << '\n';
    return 0;
}

|LIS| = 3

简要题意

求长度为n,每个元素在1∼m之间的数列个数,使得最长上升子序列长度为3,结果对998244353取模。

思路

LIS的贪心数组记录了每个长度的最小末尾值。

用p_1,p_2,p_3分别为1、2、3的LIS的最小末尾值,若某个长度不存在,记为m+1。

状态dp[i][p_1][p_2][p_3]表示填完前i个数后,贪心数组为(p_1,p_2,p_3) 的方案数。

初始状态dp[0][M+1][M+1][M+1]=1。

转移时枚举下一个数字v(1\le v \le M):

若v<p_1,更新p_1为v;

否则若p_1<v<p_2,更新p_2为v;

否则若p_2<v<p_3,更新p_3为v;

否则若v=p_1或v=p_2或v=p_3,贪心数组保持不变;

否则(v\ge p_3且不等于p_3),LIS长度会变为4,该转移非法,舍弃。

特别地,当v等于某个p_i时,贪心数组不应变化,否则会破坏严格递增性(例如v=p_1时如果错误地去更新p_2,会导致p_1=p_2,状态不合法)。

最后统计所有满足1\le p_1<p_2<p_3\le m的状态之和,即为答案。

100分


#include<bits/stdc++.h>
using namespace std;
#define int long long

const int MOD = 998244353; const int M = 12;

int dp[1005][M][M][M];

signed main() { ios::sync_with_stdio(0); cin.tie(0);

int n, m;
cin >> n >> m;
dp[0][m + 1][m + 1][m + 1] = 1;

for (int i = 0; i < n; i++) {
    for (int p1 = 1; p1 <= m + 1; p1++) {
        for (int p2 = p1; p2 <= m + 1; p2++) {
            for (int p3 = p2; p3 <= m + 1; p3++) {
                int cur = dp[i][p1][p2][p3];
                if (!cur) continue;

                for (int v = 1; v <= m; v++) {
                    if (v < p1) {
                        dp[i+1][v][p2][p3] = (dp[i+1][v][p2][p3] + cur) % MOD;
                    } else if (v > p1 && v < p2) {
                        dp[i+1][p1][v][p3] = (dp[i+1][p1][v][p3] + cur) % MOD;
                    } else if (v > p2 && v < p3) {
                        dp[i+1][p1][p2][v] = (dp[i+1][p1][p2][v] + cur) % MOD;
                    } else if (v == p1 || v == p2 || v == p3) {
                        // 状态保持不变,LIS 长度不会增加
                        dp[i+1][p1][p2][p3] = (dp[i+1][p1][p2][p3] + cur) % MOD;
                    }
                }
            }
        }
    }
}

int ans = 0;
for (int p1 = 1; p1 <= m; p1++) {
    for (int p2 = p1 + 1; p2 <= m; p2++) {
        for (int p3 = p2 + 1; p3 <= m; p3++) {
            ans = (ans + dp[n][p1][p2][p3]) % MOD;
        }
    }
}

cout << ans << '\n';
return 0;

}