結果

問題 No.27 板の準備
ユーザー 明智重蔵明智重蔵
提出日時 2015-08-02 19:13:52
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 3,433 bytes
コンパイル時間 2,575 ms
コンパイル使用メモリ 111,196 KB
実行使用メモリ 30,296 KB
最終ジャッジ日時 2023-09-25 00:35:07
合計ジャッジ時間 4,717 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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("28 29 30 30");
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("2 3 7 15");
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("3 6 11 20");
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    //必要な板の長さの配列
    static int[] NeedLenArr;

    static void Main()
    {
        List<string> InputList = GetInputList();
        NeedLenArr = InputList[0].Split(' ').Select(X => int.Parse(X)).ToArray();
        List<int[]> LenCombiArrList = DeriveLenCombiArrList(NeedLenArr.Max());

        int AnswerCnt = int.MaxValue;
        string AnswerItaCombi = "";
        foreach (int[] EachCombiArr in LenCombiArrList) {
            bool FoundAnswer;
            int MinItaCnt;

            DeriveNeedMinCnt(EachCombiArr, out FoundAnswer, out MinItaCnt);
            if (FoundAnswer == false) continue;
            if (AnswerCnt < MinItaCnt) continue;
            AnswerCnt = MinItaCnt;
            AnswerItaCombi = "";
            Array.ForEach(EachCombiArr, X => AnswerItaCombi += X.ToString() + ",");
        }
        Console.WriteLine(AnswerCnt);
        Console.WriteLine("ABCの板の長さは{0}", AnswerItaCombi);
    }

    //長さの組み合わせを求める
    static List<int[]> DeriveLenCombiArrList(int pMaxLen)
    {
        var WillReturn = new List<int[]>();

        for (int A = 1; A <= pMaxLen; A++) {
            for (int B = A; B <= pMaxLen; B++) {
                for (int C = B; C <= pMaxLen; C++) {
                    var wkList = new int[] { A, B, C };
                    int[] WillAdd = wkList.Distinct().ToArray();
                    WillReturn.Add(WillAdd);
                }
            }
        }
        return WillReturn;
    }

    //4つの長さを満たす、最小の板の枚数を動的計画法で求める
    static void DeriveNeedMinCnt(int[] pLenCombiArr, out bool pFoundAnswer,
                                 out int pMinItaCnt)
    {
        //添字が長さの合計、値が最小の枚数
        int UB = NeedLenArr.Max();
        Nullable<int>[] ItaCntArr = new Nullable<int>[UB + 1];

        //初期設定
        ItaCntArr[0] = 0;

        foreach (int EachLen in pLenCombiArr) {
            for (int I = 0; I <= UB; I++) { //123ナップサックなのでリバースループしない
                //長さの和を求める
                if (ItaCntArr[I] == null) continue;
                int wkSumLen = I + EachLen;
                if (wkSumLen > UB) break;

                //枚数の和を求める
                int wkSumCnt = (ItaCntArr[I] ?? 0) + 1;

                //枚数の最小値で更新可能かを判定
                if (ItaCntArr[wkSumLen] == null
                 || ItaCntArr[wkSumLen] > wkSumCnt) {
                    ItaCntArr[wkSumLen] = wkSumCnt;
                }
            }
        }

        pFoundAnswer = Array.TrueForAll(NeedLenArr, X => ItaCntArr[X] != null);
        pMinItaCnt = NeedLenArr.Sum(X => ItaCntArr[X] ?? 0);
    }
}
0