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

ABC080-C Shopping Street


問題へのリンク


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("1");
            WillReturn.Add("1 1 0 1 0 0 0 1 0 1");
            WillReturn.Add("3 4 5 6 7 8 9 -2 -3 4 -2");
            //8
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("2");
            WillReturn.Add("1 1 1 1 1 0 0 0 0 0");
            WillReturn.Add("0 0 0 0 0 1 1 1 1 1");
            WillReturn.Add("0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1");
            WillReturn.Add("0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1");
            //-2
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("3");
            WillReturn.Add("1 1 1 1 1 1 0 0 1 1");
            WillReturn.Add("0 1 0 1 1 1 1 0 1 0");
            WillReturn.Add("1 0 1 1 0 1 0 1 0 1");
            WillReturn.Add("-8 6 -2 -8 -8 4 8 7 -6 2 2");
            WillReturn.Add("-9 2 0 1 7 -5 0 -2 -6 5 5");
            WillReturn.Add("6 -6 7 -9 6 -5 8 0 -9 -7 -7");
            //23
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    const int UB = 9; // 0が月曜の午前、9が金曜の午後に対応する

    static void Main()
    {
        List<string> InputList = GetInputList();
        int N = int.Parse(InputList[0]);

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

        // Fの入力を受け取る
        var FArrList = new List<int[]>();
        foreach (string EachStr in InputList.Skip(1).Take(N)) {
            SplitAct(EachStr);
            FArrList.Add((int[])wkArr.Clone());
        }

        // Pの入力を受け取る
        var PArrList = new List<int[]>();
        foreach (string EachStr in InputList.Skip(1 + N)) {
            SplitAct(EachStr);
            PArrList.Add((int[])wkArr.Clone());
        }

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

        // 最低1つの時間は、選択する必要がある
        IndSetList.RemoveAll(pX => pX.Count == 0);

        int Answer = int.MinValue;
        foreach (HashSet<int> EachIndSet in IndSetList) {
            int AnswerKouho = 0;
            for (int I = 0; I <= FArrList.Count - 1; I++) {

                // 重複する時間帯の数を求める
                int DupTimeCnt = 0;

                foreach (int EachTimeInd in EachIndSet) {
                    if (FArrList[I][EachTimeInd] == 1) {
                        DupTimeCnt++;
                    }
                }
                AnswerKouho += PArrList[I][DupTimeCnt];
            }
            Answer = Math.Max(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);

        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;
    }
}


解説

10個の時間帯の、営業有無の組合せを全探索してます。