結果

問題 No.115 遠足のおやつ
ユーザー 明智重蔵明智重蔵
提出日時 2015-08-15 05:40:43
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 389 ms / 5,000 ms
コード長 3,569 bytes
コンパイル時間 2,598 ms
コンパイル使用メモリ 108,624 KB
実行使用メモリ 30,928 KB
最終ジャッジ日時 2023-08-31 00:34:52
合計ジャッジ時間 9,684 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
21,864 KB
testcase_01 AC 87 ms
22,592 KB
testcase_02 AC 89 ms
22,768 KB
testcase_03 AC 66 ms
23,912 KB
testcase_04 AC 283 ms
27,500 KB
testcase_05 AC 91 ms
22,756 KB
testcase_06 AC 87 ms
20,412 KB
testcase_07 AC 89 ms
22,676 KB
testcase_08 AC 65 ms
21,860 KB
testcase_09 AC 66 ms
21,856 KB
testcase_10 AC 65 ms
24,016 KB
testcase_11 AC 65 ms
23,864 KB
testcase_12 AC 66 ms
23,844 KB
testcase_13 AC 86 ms
24,620 KB
testcase_14 AC 94 ms
24,564 KB
testcase_15 AC 194 ms
27,004 KB
testcase_16 AC 262 ms
29,232 KB
testcase_17 AC 131 ms
26,736 KB
testcase_18 AC 65 ms
21,848 KB
testcase_19 AC 89 ms
22,584 KB
testcase_20 AC 91 ms
22,656 KB
testcase_21 AC 67 ms
21,788 KB
testcase_22 AC 142 ms
28,940 KB
testcase_23 AC 89 ms
22,580 KB
testcase_24 AC 136 ms
27,080 KB
testcase_25 AC 122 ms
24,792 KB
testcase_26 AC 98 ms
22,708 KB
testcase_27 AC 113 ms
26,732 KB
testcase_28 AC 99 ms
22,636 KB
testcase_29 AC 111 ms
24,592 KB
testcase_30 AC 134 ms
26,816 KB
testcase_31 AC 102 ms
24,820 KB
testcase_32 AC 213 ms
26,884 KB
testcase_33 AC 88 ms
22,820 KB
testcase_34 AC 275 ms
29,716 KB
testcase_35 AC 124 ms
26,740 KB
testcase_36 AC 123 ms
22,836 KB
testcase_37 AC 220 ms
28,804 KB
testcase_38 AC 388 ms
30,928 KB
testcase_39 AC 389 ms
29,000 KB
testcase_40 AC 66 ms
21,936 KB
testcase_41 AC 65 ms
21,920 KB
testcase_42 AC 86 ms
22,648 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 = "Input4";

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

        if (InputPattern == "Input1") {
            WillReturn.Add("10 9 3");
            //1 2 6
            //ルールを満たす御菓子の組み合わせは
            //(1円,2円,6円)
            //(1円,3円,5円)
            //(2円,3円,4円)
            //の3つがありますが、
            //このうち辞書順が最小になる(1円,2円,6円)の組み合わせが答えになります。
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("10 9 4");
            //-1
            //残念ながら直樹くんは御菓子を買うことができませんでした・・・
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("30 40 4");
            //1 2 7 30
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        int[] wkArr = InputList[0].Split(' ').Select(X => int.Parse(X)).ToArray();
        int N = wkArr[0];
        int D = wkArr[1];
        int K = wkArr[2];

        //[御菓子の数,金額合計]までの辞書式最小パスの2次元配列
        string[,] CurrDP = new string[K + 1, D + 1];
        string[,] PrevDP = new string[K + 1, D + 1];
        PrevDP[0, 0] = ""; //初期化
        
        Array.Copy(PrevDP, CurrDP, PrevDP.Length);
        for (int LoopN = 1; LoopN <= N; LoopN++) {
            for (int LoopK = 0; LoopK <= K - 1; LoopK++) {
                for (int LoopD = 0; LoopD <= D; LoopD++) {
                    if (PrevDP[LoopK, LoopD] == null) continue;
                    int NewIndK = LoopK + 1;
                    int NewIndD = LoopD + LoopN;
                    if (NewIndD > D) continue;
                    string NewPath = PrevDP[LoopK, LoopD]
                        + " " + LoopN.ToString().PadLeft(3, '0');

                    if (CurrDP[NewIndK, NewIndD] == null
                     || CurrDP[NewIndK, NewIndD].CompareTo(NewPath) > 0) {
                        CurrDP[NewIndK, NewIndD] = NewPath;
                    }
                }
            }
            //Console.WriteLine(new string('■', 15));
            //Console.WriteLine("{0}円の御菓子までのDPの結果", LoopN);
            //PrintDP(CurrDP);

            Array.Copy(CurrDP, PrevDP, CurrDP.Length);
        }

        if (CurrDP[K, D] != null) {
            int[] AnswerIntArr = CurrDP[K, D].Trim().Split(' ').
                Select(X => int.Parse(X)).ToArray();
            string[] AnswerStrArr = Array.ConvertAll(AnswerIntArr, X => X.ToString());
            Console.WriteLine(string.Join(" ", AnswerStrArr));
        }
        else Console.WriteLine(-1);
    }

    static void PrintDP(string[,] pCurrDP)
    {
        var sb = new System.Text.StringBuilder();
        for (int LoopK = 0; LoopK <= pCurrDP.GetUpperBound(0); LoopK++) {
            for (int LoopD = 0; LoopD <= pCurrDP.GetUpperBound(1); LoopD++) {
                if (pCurrDP[LoopK, LoopD] == null) continue;
                sb.AppendFormat("{0}個で{1}円の辞書順最小パスは{2}",
                    LoopK, LoopD, pCurrDP[LoopK, LoopD]);
                sb.AppendLine();
            }
        }
        Console.WriteLine(sb.ToString());
    }
}
0