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

ABC364-E Maximum Glutton


問題へのリンク


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 8 4");
            WillReturn.Add("1 5");
            WillReturn.Add("3 2");
            WillReturn.Add("4 1");
            WillReturn.Add("5 3");
            //3
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("2 1 1");
            WillReturn.Add("3 2");
            WillReturn.Add("3 2");
            //1
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("2 100 100");
            WillReturn.Add("3 2");
            WillReturn.Add("3 2");
            //2
        }
        else if (InputPattern == "Input4") {
            WillReturn.Add("6 364 463");
            WillReturn.Add("230 381");
            WillReturn.Add("154 200");
            WillReturn.Add("328 407");
            WillReturn.Add("339 94");
            WillReturn.Add("193 10");
            WillReturn.Add("115 309");
            //3
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct ItemInfoDef
    {
        internal long X;
        internal long Y;
    }
    static List<ItemInfoDef> mItemInfoList = new List<ItemInfoDef>();

    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 N = wkArr[0];
        long XLimit = wkArr[1];
        long YLimit = wkArr[2];

        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            ItemInfoDef WillAdd;
            WillAdd.X = wkArr[0];
            WillAdd.Y = wkArr[1];
            mItemInfoList.Add(WillAdd);
        }

        // Yの最小値[個数][Xの合計]なインラインDP表
        long?[,] DPArr = new long?[N + 1, XLimit + 1];
        DPArr[0, 0] = 0;

        foreach (ItemInfoDef EachItem in mItemInfoList) {
            for (long I = N; 0 <= I; I--) {
                for (long J = XLimit; 0 <= J; J--) {
                    if (DPArr[I, J].HasValue == false) continue;

                    long NewI = I + 1;
                    long NewJ = J + EachItem.X;
                    long NewVal = DPArr[I, J].Value + EachItem.Y;

                    if (NewJ > XLimit || NewVal > YLimit) {
                        continue;
                    }

                    if (DPArr[NewI, NewJ].HasValue) {
                        if (DPArr[NewI, NewJ] <= NewVal) {
                            continue;
                        }
                    }
                    DPArr[NewI, NewJ] = NewVal;
                }
            }
        }

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

                long AnswerKouho = I + 1;
                AnswerKouho = Math.Min(N, AnswerKouho);
                AnswerList.Add(AnswerKouho);
            }
        }
        Console.WriteLine(AnswerList.Max());
    }
}


解説

Yの最小値[個数][Xの合計]でインラインDPを行い、
最後に解を求めてます。