AtCoderのABC    次のABCの問題へ    前のABCの問題へ

ABC237-F |LIS| = 3


問題へのリンク


C#のソース

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("4 5");
            //135
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3 4");
            //4
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("111 3");
            //144980434
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    const int Hou = 998244353;

    static void Main()
    {
        List<string> InputList = GetInputList();
        int[] wkArr = InputList[0].Split(' ').Select(pX => int.Parse(pX)).ToArray();
        int N = wkArr[0];
        int M = wkArr[1];

        int UB = M;

        // 場合の数[LISの1項目の最小値,LISの2項目の最小値,LISの3項目の最小値] なDP表
        int[, ,] PrevDP = new int[UB + 1, UB + 1, UB + 1];
        PrevDP[0, 0, 0] = 1;

        for (int I = 1; I <= N; I++) {
            int[, ,] CurrDP = new int[UB + 1, UB + 1, UB + 1];

            for (int J = 0; J <= M; J++) {
                for (int K = 0; K <= M; K++) {
                    for (int L = 0; L <= M; L++) {
                        if (PrevDP[J, K, L] == 0) continue;

                        for (int NewVal = 1; NewVal <= M; NewVal++) {
                            // LISの長さが4になるのはNG
                            if (0 < L && L < NewVal) break;

                            int NewJ;
                            int NewK;
                            int NewL;
                            DeriveNewInd(NewVal, J, K, L, out NewJ, out NewK, out NewL);
                            CurrDP[NewJ, NewK, NewL] += PrevDP[J, K, L];
                            CurrDP[NewJ, NewK, NewL] %= Hou;
                        }
                    }
                }
            }
            PrevDP = CurrDP;
        }

        long Answer = 0;
        for (int I = 1; I <= UB; I++) {
            for (int J = 1; J <= UB; J++) {
                for (int K = 1; K <= UB; K++) {
                    Answer += PrevDP[I, J, K];
                    Answer %= Hou;
                }
            }
        }
        Console.WriteLine(Answer);
    }

    // 数列の新しい値と
    // J,K,Lを引数として
    // NewJ,NewK,NewLを返す
    static void DeriveNewInd(int pNewVal,
        int pJ, int pK, int pL, 
        out int pNewJ, out int pNewK, out int pNewL)
    {
        pNewJ = pJ;
        pNewK = pK;
        pNewL = pL;

        int[] ValArr = new int[3];
        ValArr[0] = pJ;
        ValArr[1] = pK;
        ValArr[2] = pL;

        for (int I = 0; I <= 2; I++) {
            if (ValArr[I] == 0) ValArr[I] = int.MaxValue;
        }

        if (pNewVal < ValArr[0]) {
            pNewJ = pNewVal;
        }
        if (ValArr[0] < pNewVal && pNewVal < ValArr[1]) {
            pNewK = pNewVal;
        }
        if (ValArr[1] < pNewVal && pNewVal < ValArr[2]) {
            pNewL = pNewVal;
        }
    }
}


解説

LISを求めるDPの感覚で
場合の数[LISの1項目の最小値,LISの2項目の最小値,LISの3項目の最小値]
を更新するDPで解いてます。