結果

問題 No.27 板の準備
ユーザー 明智重蔵明智重蔵
提出日時 2015-08-02 19:14:57
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 75 ms / 5,000 ms
コード長 3,435 bytes
コンパイル時間 6,827 ms
コンパイル使用メモリ 111,924 KB
実行使用メモリ 30,056 KB
最終ジャッジ日時 2023-08-27 04:04:04
合計ジャッジ時間 5,747 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
21,852 KB
testcase_01 AC 69 ms
21,824 KB
testcase_02 AC 75 ms
24,048 KB
testcase_03 AC 68 ms
19,780 KB
testcase_04 AC 72 ms
25,892 KB
testcase_05 AC 69 ms
19,724 KB
testcase_06 AC 70 ms
23,960 KB
testcase_07 AC 74 ms
26,060 KB
testcase_08 AC 75 ms
26,180 KB
testcase_09 AC 75 ms
30,056 KB
testcase_10 AC 75 ms
28,064 KB
testcase_11 AC 71 ms
25,928 KB
testcase_12 AC 72 ms
25,948 KB
testcase_13 AC 71 ms
21,924 KB
testcase_14 AC 71 ms
21,940 KB
testcase_15 AC 75 ms
28,092 KB
testcase_16 AC 68 ms
21,932 KB
testcase_17 AC 75 ms
25,928 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("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