AtCoderのARC    次のARCの問題へ    前のARCの問題へ

ARC133-B Dividing Subsequence


問題へのリンク


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");
            WillReturn.Add("3 1 4 2");
            WillReturn.Add("4 2 1 3");
            //2
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("5");
            WillReturn.Add("1 2 3 4 5");
            WillReturn.Add("5 4 3 2 1");
            //3
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("10");
            WillReturn.Add("4 3 1 10 9 2 8 6 5 7");
            WillReturn.Add("9 6 5 4 2 3 8 10 1 7");
            //6
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        int[] PArr = InputList[1].Split(' ').Select(pX => int.Parse(pX)).ToArray();
        int[] QArr = InputList[2].Split(' ').Select(pX => int.Parse(pX)).ToArray();

        // QのInd[Qの値]なDict
        var QIndDict = new Dictionary<int, int>();
        for (int I = 0; I <= QArr.GetUpperBound(0); I++) {
            QIndDict[QArr[I]] = I;
        }

        // 可能なQのIndのList [Pの値] なDict
        var QIndListDict = new Dictionary<int, List<int>>();

        foreach (int EachQ in QArr) {
            int[] YakusuuArr = DeriveYakusuuArr(EachQ);
            foreach (int EachYakusuu in YakusuuArr) {
                if (QIndListDict.ContainsKey(EachYakusuu) == false) {
                    QIndListDict[EachYakusuu] = new List<int>();
                }
                QIndListDict[EachYakusuu].Add(QIndDict[EachQ]);
            }
        }

        var ValList = new List<int>();
        foreach (int EachP in PArr) {
            if (QIndListDict.ContainsKey(EachP)) {
                for (int I = QIndListDict[EachP].Count - 1; 0 <= I; I--) {
                    ValList.Add(QIndListDict[EachP][I]);
                }
            }
        }

        int LISLen = DeriveLISLen(ValList);
        Console.WriteLine(LISLen);
    }

    // 列挙を引数として、LISの長さを返す
    static int DeriveLISLen(IEnumerable<int> pEnum)
    {
        // LISの最終値の最小値[LISの長さ] なDP表
        var DPSortedList = new SortedList<int, int>();

        foreach (int EachA in pEnum) {
            if (DPSortedList.Count == 0) {
                DPSortedList[1] = EachA;
                continue;
            }
            int UpsertKeyInd = ExecNibunhou(DPSortedList, EachA);

            int CurrUB = DPSortedList.Count - 1;
            var Keys = DPSortedList.Keys;

            // 更新する位置によって分岐
            if (UpsertKeyInd <= CurrUB) {
                DPSortedList[Keys[UpsertKeyInd]] = EachA;
            }
            else {
                int PrevKey = Keys[CurrUB];
                DPSortedList[PrevKey + 1] = EachA;
            }
        }

        return DPSortedList.Keys.Max();
    }

    // 二分法で、引数の値を設定する、キーの配列の添字を返す
    static int ExecNibunhou(SortedList<int, int> pDPSortedList, int pTargetVal)
    {
        int UB = pDPSortedList.Count - 1;
        var Keys = pDPSortedList.Keys;

        // 最小値以下の場合
        if (pTargetVal <= pDPSortedList[Keys[0]]) {
            return 0;
        }

        // 最大値より大きい場合
        if (pTargetVal > pDPSortedList[Keys[UB]]) {
            return UB + 1;
        }

        int L = 0;
        int R = UB;

        while (L + 1 < R) {
            int Mid = (L + R) / 2;
            if (pDPSortedList[Keys[Mid]] < pTargetVal) {
                L = Mid;
            }
            else {
                R = Mid;
            }
        }
        return R;
    }

    // 約数を列挙する
    static int[] DeriveYakusuuArr(int pTarget)
    {
        var YakusuuSet = new HashSet<int>();
        for (int I = 1; I * I <= pTarget; I++) {
            if (pTarget % I == 0) {
                YakusuuSet.Add(I);
                YakusuuSet.Add(pTarget / I);
            }
        }
        int[] YakusuuArr = YakusuuSet.ToArray();
        Array.Sort(YakusuuArr);
        return YakusuuArr;
    }
}


解説

考察すると、LISの長さを求める問題に帰着できます。


類題

DPL_1_D: Longest Increasing Subsequence