DPコンテスト    次のDPコンテストの問題へ    前のDPコンテストの問題へ

Educational DP Contest C Vacation


問題へのリンク


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

    struct ABCDef
    {
        internal int A;
        internal int B;
        internal int C;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();

        int[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(X => int.Parse(X)).ToArray();

        var ABCList = new List<ABCDef>();

        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            ABCDef WillAdd;
            WillAdd.A = wkArr[0];
            WillAdd.B = wkArr[1];
            WillAdd.C = wkArr[2];
            ABCList.Add(WillAdd);
        }

        int PrevASumMax = 0; // 直近でAを選択した際の幸福度合計の最大値
        int PrevBSumMax = 0; // 直近でBを選択した際の幸福度合計の最大値
        int PrevCSumMax = 0; // 直近でCを選択した際の幸福度合計の最大値

        for (int I = 0; I <= ABCList.Count - 1; I++) {
            Func<int, int, int, int> wkFunc = (pPrevSum1, pPrevSum2, CurrVal) =>
            {
                return Math.Max(pPrevSum1 + CurrVal, pPrevSum2 + CurrVal);
            };

            int NewPrevASumMax = wkFunc(PrevBSumMax, PrevCSumMax, ABCList[I].A);
            int NewPrevBSumMax = wkFunc(PrevASumMax, PrevCSumMax, ABCList[I].B);
            int NewPrevCSumMax = wkFunc(PrevASumMax, PrevBSumMax, ABCList[I].C);

            PrevASumMax = NewPrevASumMax;
            PrevBSumMax = NewPrevBSumMax;
            PrevCSumMax = NewPrevCSumMax;
        }
        int Answer = Math.Max(PrevASumMax, PrevBSumMax);
        Answer = Math.Max(Answer, PrevCSumMax);
        Console.WriteLine(Answer);
    }
}


解説

DPで解いてます。