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

ABC338-C Leftover Recipes


問題へのリンク


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("2");
            WillReturn.Add("800 300");
            WillReturn.Add("100 100");
            WillReturn.Add("200 10");
            //5
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("2");
            WillReturn.Add("800 300");
            WillReturn.Add("100 0");
            WillReturn.Add("0 10");
            //38
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("2");
            WillReturn.Add("800 300");
            WillReturn.Add("801 300");
            WillReturn.Add("800 301");
            //0
        }
        else if (InputPattern == "Input4") {
            WillReturn.Add("10");
            WillReturn.Add("1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000");
            WillReturn.Add("0 1 2 3 4 5 6 7 8 9");
            WillReturn.Add("9 8 7 6 5 4 3 2 1 0");
            //222222
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static int UB;
    static int[] mQArr;
    static int[] mAArr;
    static int[] mBArr;

    static void Main()
    {
        List<string> InputList = GetInputList();
        mQArr = InputList[1].Split(' ').Select(pX => int.Parse(pX)).ToArray();
        mAArr = InputList[2].Split(' ').Select(pX => int.Parse(pX)).ToArray();
        mBArr = InputList[3].Split(' ').Select(pX => int.Parse(pX)).ToArray();
        UB = mQArr.GetUpperBound(0);

        // 料理Aを最大で何個作れるか
        var DivList1 = new List<int>();
        for (int I = 0; I <= UB; I++) {
            if (mAArr[I] == 0) continue;
            DivList1.Add(mQArr[I] / mAArr[I]);
        }
        int MaxCnt = DivList1.Min();
        //Console.WriteLine("料理Aは最大{0}個作れる", MaxCnt);

        var AnswerList = new List<int>();
        for (int I = 0; I <= MaxCnt; I++) {
            int AnswerKouho = I;

            int[] CopyQArr = (int[])mQArr.Clone();
            for (int J = 0; J <= UB; J++) {
                CopyQArr[J] -= I * mAArr[J];
            }

            var DivList2 = new List<int>();
            for (int J = 0; J <= UB; J++) {
                if (mBArr[J] == 0) continue;
                DivList2.Add(CopyQArr[J] / mBArr[J]);
            }
            AnswerKouho += DivList2.Min();
            AnswerList.Add(AnswerKouho);
        }
        Console.WriteLine(AnswerList.Max());
    }
}


解説

料理Aを作る個数を全探索してます。