結果

問題 No.115 遠足のおやつ
ユーザー 明智重蔵
提出日時 2015-08-13 07:57:54
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 3,319 bytes
コンパイル時間 841 ms
コンパイル使用メモリ 114,396 KB
実行使用メモリ 38,364 KB
最終ジャッジ日時 2024-07-18 09:11:44
合計ジャッジ時間 7,433 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 3
other WA * 4 TLE * 1 -- * 35
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;
    }

    struct JyoutaiDef
    {
        internal int CurrN;
        internal List<int> SelectedList;
        internal int SumVal;
    }

    //メモ化探索用
    struct MemoDef
    {
        internal int CurrN;
        internal int SelectedCnt;
        internal int SumVal;
    }

    //No.115 遠足のおやつ
    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];

        var stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrN = 1;
        WillPush.SelectedList = new List<int>();
        WillPush.SumVal = 0;
        stk.Push(WillPush);

        var MemoList = new List<MemoDef>();

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

            //クリア判定
            if (Popped.SelectedList.Count == K) {
                if (Popped.SumVal == D) {
                    Console.WriteLine(
                    string.Join(" ", Popped.SelectedList.Select(X => X.ToString()).ToArray()));
                    return;
                }
                continue;
            }

            for (int I = N; Popped.CurrN <= I; I--) {
                WillPush.CurrN = I + 1;
                WillPush.SelectedList = new List<int>(Popped.SelectedList) { I };
                WillPush.SumVal = Popped.SumVal + I;

                //解でないのに半分を超えたら枝切り
                if (WillPush.SumVal != D && WillPush.SumVal > D / 2) {
                    continue;
                }

                //メモ化探索
                MemoDef MemoInfo;
                MemoInfo.CurrN = WillPush.CurrN;
                MemoInfo.SelectedCnt = WillPush.SelectedList.Count;
                MemoInfo.SumVal = WillPush.SumVal;
                if (MemoList.Exists(X => X.CurrN == MemoInfo.CurrN
                    && X.SelectedCnt == MemoInfo.SelectedCnt
                    && X.SumVal == MemoInfo.SumVal)) continue;
                MemoList.Add(MemoInfo);

                stk.Push(WillPush);
            }
        }
    }
}
0