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

ABC104-C All Green


問題へのリンク


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 700");
            WillReturn.Add("3 500");
            WillReturn.Add("5 800");
            //3
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("2 2000");
            WillReturn.Add("3 500");
            WillReturn.Add("5 800");
            //7
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("2 400");
            WillReturn.Add("3 500");
            WillReturn.Add("5 800");
            //2
        }
        else if (InputPattern == "Input4") {
            WillReturn.Add("5 25000");
            WillReturn.Add("20 1000");
            WillReturn.Add("40 1000");
            WillReturn.Add("50 1000");
            WillReturn.Add("30 1000");
            WillReturn.Add("1 1000");
            //66
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct PCInfoDef
    {
        internal int Score;
        internal int ProblemCnt;
        internal int CompBonus;
    }
    static List<PCInfoDef> mPCInfoList = new List<PCInfoDef>();

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

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

        SplitAct(InputList[0]);
        int G = wkArr[1];

        for (int I = 1; I <= InputList.Count - 1; I++) {
            SplitAct(InputList[I]);
            PCInfoDef WillAdd;
            WillAdd.Score = I * 100;
            WillAdd.ProblemCnt = wkArr[0];
            WillAdd.CompBonus = wkArr[1];
            mPCInfoList.Add(WillAdd);
        }

        List<HashSet<int>> IndSetList = ExecDFS();

        //foreach (HashSet<int> EachIndSet in IndSetList) {
        //    foreach (int EachInd in EachIndSet) {
        //        Console.Write("{0},", EachInd);
        //    }
        //    Console.WriteLine();
        //}

        int Answer = int.MaxValue;
        foreach (HashSet<int> EachIndSet in IndSetList) {
            int AnswerKouho = 0;
            int ScoreSum = 0;

            // 全完した点数の処理
            foreach (int EachInd in EachIndSet) {
                ScoreSum += mPCInfoList[EachInd].Score * mPCInfoList[EachInd].ProblemCnt;
                ScoreSum += mPCInfoList[EachInd].CompBonus;
                AnswerKouho += mPCInfoList[EachInd].ProblemCnt;
            }

            if (ScoreSum < G) {
                // 全完してない一番点数の高い問題を、全完せずに解く
                for (int I = mPCInfoList.Count - 1; 0 <= I; I--) {
                    if (EachIndSet.Contains(I)) continue;

                    for (int J = 1; J <= mPCInfoList[I].ProblemCnt - 1; J++) {
                        ScoreSum += mPCInfoList[I].Score;
                        AnswerKouho++;
                        if (ScoreSum >= G) break;
                    }
                    break;
                }
            }
            if (ScoreSum >= G) {
                Answer = Math.Min(Answer, AnswerKouho);
            }
        }
        Console.WriteLine(Answer);
    }

    struct JyoutaiDef
    {
        internal int CurrInd;
        internal HashSet<int> IndSet;
    }

    // DFSで満点を取る添字SetのListを返す
    static List<HashSet<int>> ExecDFS()
    {
        var WillReturn = new List<HashSet<int>>();

        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrInd = 0;
        WillPush.IndSet = new HashSet<int>();
        Stk.Push(WillPush);

        int UB = mPCInfoList.Count - 1;
        while (Stk.Count > 0) {
            JyoutaiDef Popped = Stk.Pop();
            WillReturn.Add(Popped.IndSet);

            if (Popped.CurrInd > UB) {
                continue;
            }

            for (int I = Popped.CurrInd; I <= UB; I++) {
                WillPush.CurrInd = I + 1;
                WillPush.IndSet = new HashSet<int>(Popped.IndSet) { I };
                Stk.Push(WillPush);
            }
        }
        return WillReturn;
    }
}


解説

最初に、点数ごとに全完の有無を、深さ優先探索で列挙してます。

次に、全完の有無の組合せごとに
全完した点数での合計を求め、それでも目標点に足りない場合は、
全完してない一番点数の高い問題を、全完せずに解くシュミレーションをしてます。