競技プログラミングの鉄則    次の問題へ    前の問題へ

B17 Frog 1 with Restoration


問題へのリンク


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

    static void Main()
    {
        List<string> InputList = GetInputList();
        long[] HArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();
        long UB = HArr.GetUpperBound(0);

        // 最小コスト[現在位置] なインラインDP表
        long?[] DPArr = new long?[UB + 1];
        DPArr[0] = 0;

        while (true) {
            bool Updated = false;
            for (int I = 0; I <= UB; I++) {
                if (DPArr[I].HasValue == false) continue;

                Action<long> UpdateAct = (pNewI) =>
                {
                    if (pNewI > UB) return;

                    long NewVal = DPArr[I].Value;
                    NewVal += Math.Abs(HArr[I] - HArr[pNewI]);

                    if (DPArr[pNewI].HasValue) {
                        if (DPArr[pNewI] <= NewVal) {
                            return;
                        }
                    }
                    Updated = true;
                    DPArr[pNewI] = NewVal;
                };
                UpdateAct(I + 1);
                UpdateAct(I + 2);
            }
            if (Updated == false) break;
        }

        // DP復元
        var IndList = new List<long>();
        long CurrInd = UB;
        while (true) {
            IndList.Add(CurrInd);
            if (CurrInd == 0) break;

            Func<long, bool> UpdateFunc = (pNewInd) =>
            {
                if (pNewInd < 0) return false;
                if (DPArr[pNewInd].HasValue == false) return false;
                long CorrectVal = DPArr[pNewInd].Value;

                long NewVal = DPArr[CurrInd].Value;
                NewVal -= Math.Abs(HArr[CurrInd] - HArr[pNewInd]);

                if (NewVal == CorrectVal) {
                    CurrInd = pNewInd;
                    return true;
                }
                return false;
            };

            if (UpdateFunc(CurrInd - 1)) continue;
            if (UpdateFunc(CurrInd - 2)) continue;
        }

        for (int I = 0; I <= IndList.Count - 1; I++) {
            IndList[I]++;
        }
        IndList.Reverse();

        Console.WriteLine(IndList.Count);
        Console.WriteLine(LongEnumJoin(" ", IndList));
    }

    // セパレータとLong型の列挙を引数として、結合したstringを返す
    static string LongEnumJoin(string pSeparater, IEnumerable<long> pEnum)
    {
        string[] StrArr = Array.ConvertAll(pEnum.ToArray(), pX => pX.ToString());
        return string.Join(pSeparater, StrArr);
    }
}


解説

DP復元を行ってます。