Official

F - |LIS| = 3 Editorial by en_translator


Consider the following DP (Dynamic Programming):

\(dp[i][a_1][a_2][a_3] :=\) The number of sequences of length \(i\) such that the minimum possible \(j\)-th element of longest increasing subsequence of length \(j\)

then, there are \(O(NM^3)\) number of states, and each transition cost \(O(M)\) time, so the problem can be solved in a total of \(O(NM^4)\) time.

Please refer to the following sample code if you do not figure out the initial values and transitions of \(dp\).

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
typedef long long ll;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
using mint=modint998244353;
typedef vector<mint>vi;
typedef vector<vi>vvi;
typedef vector<vvi>vvvi;
typedef vector<vvvi>vvvvi;

signed main(){
    ll n,m;cin>>n>>m;
    vvvvi dp(n+1,vvvi(m+2,vvi(m+2,vi(m+2))));
    dp[0][m+1][m+1][m+1] = 1;
    rep(i,0,n-1)rep(a1,1,m+1)rep(a2,1,m+1)rep(a3,1,m+1){
        rep(x,1,m){
            // Let x be the (i+1)-th term of the sequence
            if(x<=a1)     dp[i+1][x][a2][a3] += dp[i][a1][a2][a3];
            else if(x<=a2)dp[i+1][a1][x][a3] += dp[i][a1][a2][a3];
            else if(x<=a3)dp[i+1][a1][a2][x] += dp[i][a1][a2][a3];
        }
    }
    mint ans=0;
    rep(a1,1,m)rep(a2,a1+1,m)rep(a3,a2+1,m)ans += dp[n][a1][a2][a3];
    cout<<ans.val()<<endl;
    return 0;
}

posted:
last update: