結果

問題 No.27 板の準備
ユーザー 明智重蔵明智重蔵
提出日時 2015-08-08 16:51:19
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 76 ms / 5,000 ms
コード長 2,999 bytes
コンパイル時間 2,710 ms
コンパイル使用メモリ 110,768 KB
実行使用メモリ 28,096 KB
最終ジャッジ日時 2023-08-27 04:04:25
合計ジャッジ時間 4,737 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
23,812 KB
testcase_01 AC 68 ms
23,880 KB
testcase_02 AC 74 ms
26,048 KB
testcase_03 AC 68 ms
21,916 KB
testcase_04 AC 71 ms
23,868 KB
testcase_05 AC 70 ms
21,752 KB
testcase_06 AC 71 ms
21,860 KB
testcase_07 AC 75 ms
28,036 KB
testcase_08 AC 75 ms
26,084 KB
testcase_09 AC 75 ms
26,012 KB
testcase_10 AC 73 ms
26,132 KB
testcase_11 AC 70 ms
23,996 KB
testcase_12 AC 72 ms
24,016 KB
testcase_13 AC 72 ms
24,044 KB
testcase_14 AC 69 ms
21,824 KB
testcase_15 AC 73 ms
26,064 KB
testcase_16 AC 68 ms
21,880 KB
testcase_17 AC 76 ms
28,096 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 List<string> GetInputList()
    {
        var WillReturn = new List<string>();
        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);
    }

    //長さの組み合わせを求める
    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].Value + 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