結果

問題 No.90 品物の並び替え
ユーザー 明智重蔵明智重蔵
提出日時 2015-09-15 16:14:14
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 2,485 ms / 5,000 ms
コード長 3,530 bytes
コンパイル時間 2,641 ms
コンパイル使用メモリ 109,136 KB
実行使用メモリ 55,312 KB
最終ジャッジ日時 2023-09-26 12:04:24
合計ジャッジ時間 7,287 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
21,796 KB
testcase_01 AC 275 ms
29,124 KB
testcase_02 AC 71 ms
19,696 KB
testcase_03 AC 89 ms
26,008 KB
testcase_04 AC 96 ms
28,000 KB
testcase_05 AC 295 ms
31,124 KB
testcase_06 AC 260 ms
29,220 KB
testcase_07 AC 73 ms
21,764 KB
testcase_08 AC 70 ms
21,824 KB
testcase_09 AC 2,485 ms
55,312 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static string InputPattern = "Input2";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("4 9");
            WillReturn.Add("0 1 1");
            WillReturn.Add("0 2 2");
            WillReturn.Add("0 3 3");
            WillReturn.Add("1 2 4");
            WillReturn.Add("1 3 5");
            WillReturn.Add("2 3 6");
            WillReturn.Add("3 2 100");
            WillReturn.Add("2 1 100");
            WillReturn.Add("1 0 100");
            //300
            //3 2 1 0
            //と並べた時の300点が最大となる。
            //0 1 2 3
            //と並べた時、適用される数は6個だが、21点となり、最高点は得られないことに注意せよ。
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct TokutenDef
    {
        internal int Item1;
        internal int Item2;
        internal int Score;
    }

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

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

        var TokutenList = new List<TokutenDef>();
        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            TokutenList.Add(new TokutenDef() { Item1 = wkArr[0], Item2 = wkArr[1], Score = wkArr[2] });
        }
        TokutenDef[] TokutenArr = TokutenList.ToArray();

        //深さ優先探索で、品物の並びの順列を列挙
        List<int[]> NumArrList = ExecDFS(N);

        int MaxScoreSum = int.MinValue;
        foreach (int[] EachNumArr in NumArrList) {

            //数値ごとの存在する添字のDict
            var NumIndDict = new Dictionary<int, int>();
            for (int I = 0; I <= EachNumArr.GetUpperBound(0); I++) {
                NumIndDict[EachNumArr[I]] = I;
            }

            int ScoreSum = 0;
            foreach (TokutenDef EachTokuten in TokutenArr) {
                if (NumIndDict[EachTokuten.Item1] < NumIndDict[EachTokuten.Item2])
                    ScoreSum += EachTokuten.Score;
            }
            if (MaxScoreSum < ScoreSum)
                MaxScoreSum = ScoreSum;
        }
        Console.WriteLine(MaxScoreSum);
    }

    struct JyoutaiDef
    {
        internal List<int> NumList;
    }

    //深さ優先探索で、品物の並びの順列を列挙
    static List<int[]> ExecDFS(int pN)
    {
        var WillReturn = new List<int[]>();

        var stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        for (int I = 0; I <= pN - 1; I++) {
            WillPush.NumList = new List<int> { I };
            stk.Push(WillPush);
        }

        while (stk.Count > 0) {
            JyoutaiDef Popped = stk.Pop();

            if (Popped.NumList.Count == pN) {
                WillReturn.Add(Popped.NumList.ToArray());
                continue;
            }

            for (int I = 0; I <= pN - 1; I++) {
                if (Popped.NumList.Contains(I)) continue;
                WillPush.NumList = new List<int>(Popped.NumList) { I };
                stk.Push(WillPush);
            }
        }
        return WillReturn;
    }
}
0