AOJ本の読書メモ   AOJ    次のAOJの問題へ    前のAOJの問題へ

AOJ 0573 夜店


問題へのリンク


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("5 20 14");
            WillReturn.Add("8 9");
            WillReturn.Add("2 4");
            WillReturn.Add("7 13");
            WillReturn.Add("6 3");
            WillReturn.Add("5 8");
            //16
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct ShopInfo
    {
        internal long Score;
        internal long Elapsed;
    }
    static List<ShopInfo> mShopList = new List<ShopInfo>();

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

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

        SplitAct(InputList[0]);
        long T = wkArr[1];
        long S = wkArr[2];
        long UB0 = T;
        long UB1 = 1;

        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            ShopInfo WillAdd;
            WillAdd.Score = wkArr[0];
            WillAdd.Elapsed = wkArr[1];
            mShopList.Add(WillAdd);
        }

        // 最大スコア[現在時刻,花火を見たか]なインラインDP表
        long?[,] DPArr = new long?[UB0 + 1, UB1 + 1];
        DPArr[0, 0] = 0;

        foreach (ShopInfo EachShop in mShopList) {
            for (long I = UB0; 0 <= I; I--) {
                for (long J = UB1; 0 <= J; J--) {
                    if (DPArr[I, J].HasValue == false) continue;

                    Action<long, long, long> UpdateAct = (pNewI, pNewJ, pNewVal) =>
                    {
                        if (pNewI > UB0) return;

                        if (DPArr[pNewI, pNewJ].HasValue) {
                            if (DPArr[pNewI, pNewJ] >= pNewVal) {
                                return;
                            }
                        }
                        DPArr[pNewI, pNewJ] = pNewVal;
                    };

                    // 花火を見るまで待つ場合
                    if (I <= S) {
                        UpdateAct(S, 1, DPArr[I, J].Value);
                    }

                    // 夜店で遊ぶ場合
                    UpdateAct(I + EachShop.Elapsed, J, DPArr[I, J].Value + EachShop.Score);
                }
            }
        }

        var AnswerList = new List<long>();
        for (long I = UB0; 0 <= I; I--) {
            for (long J = UB1; 0 <= J; J--) {
                if (DPArr[I, J].HasValue == false) continue;

                if (J == 1) AnswerList.Add(DPArr[I, J].Value);

                // 最後に花火を見る場合も解候補
                if (J == 0) {
                    if (I <= S) {
                        AnswerList.Add(DPArr[I, J].Value);
                    }
                }
            }
        }
        Console.WriteLine(AnswerList.Max());
    }
}


解説

最大スコア[現在時刻,花火を見たか]でDPしてます。