結果

問題 No.27 板の準備
ユーザー 14番14番
提出日時 2016-07-15 02:50:35
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 81 ms / 5,000 ms
コード長 3,319 bytes
コンパイル時間 4,896 ms
コンパイル使用メモリ 105,904 KB
実行使用メモリ 25,932 KB
最終ジャッジ日時 2023-08-27 04:30:54
合計ジャッジ時間 4,666 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
23,740 KB
testcase_01 AC 69 ms
21,904 KB
testcase_02 AC 72 ms
23,932 KB
testcase_03 AC 70 ms
19,856 KB
testcase_04 AC 71 ms
19,692 KB
testcase_05 AC 74 ms
23,872 KB
testcase_06 AC 79 ms
23,880 KB
testcase_07 AC 74 ms
25,860 KB
testcase_08 AC 81 ms
23,872 KB
testcase_09 AC 79 ms
23,872 KB
testcase_10 AC 77 ms
23,784 KB
testcase_11 AC 74 ms
23,816 KB
testcase_12 AC 78 ms
25,932 KB
testcase_13 AC 72 ms
21,772 KB
testcase_14 AC 74 ms
25,900 KB
testcase_15 AC 81 ms
23,984 KB
testcase_16 AC 70 ms
21,900 KB
testcase_17 AC 75 ms
21,848 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.Text;
using System.Linq;

class Program
{
    public void Proc()
    {
        Reader.IsDebug = false;
        int ans = int.MaxValue;
        int[] ita = Reader.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray();
        for (int i = 1; i <= 30 - 2; i++)
        {
            for(int j=i+1; j<=30-1; j++) {
                for (int k = j + 1; k <= 30; k++)
                {
                    int cnt = 0;
                    bool cannot = false;
                    foreach (int num in ita)
                    {
                        int key = (1 << i) + (1 << j) + (1 << k);
                        int ret = this.GetAns(key, i, j, k, num);
                        if (ret == 0)
                        {
                            cannot = true;
                            break;
                        }
                        else
                        {
                            cnt += ret;
                        }
                    }
                    if (!cannot)
                    {
                        ans = Math.Min(cnt, ans);
                    }
                }
            }
        }
        Console.WriteLine(ans);
    }

    private Dictionary<int, Dictionary<int, int>> dic = new Dictionary<int, Dictionary<int, int>>();


    private int GetAns(int key, int a, int b, int c, int target)
    {
        if (!dic.ContainsKey(key))
        {
            dic.Add(key, new Dictionary<int, int>());
        }
        if (dic[key].ContainsKey(target))
        {
            return dic[key][target];
        }

        int ans = int.MaxValue;

        if(a == target || b == target || c == target) {
            ans = 1;
        } else {

            if(target > c) {
                int ret = this.GetAns(key, a, b, c, target - c);
                if (ret > 0)
                {
                    ret++;
                    ans = Math.Min(ans, ret);
                }
            } 
            if(target > b) {
                int ret = this.GetAns(key, a, b, c, target - b);
                if (ret > 0)
                {
                    ret++;
                    ans = Math.Min(ans, ret);
                }
            } 
            if(target > a) {
                int ret = this.GetAns(key, a, b, c, target - a);
                if (ret > 0)
                {
                    ret++;
                    ans = Math.Min(ans, ret);
                }
            }




        }
        if (ans == int.MaxValue)
        {
            ans = 0;
        }

        dic[key].Add(target, ans);
        return ans;
    }



    public class Reader
    {
        public static bool IsDebug = true;
        private static String PlainInput = @"


9 16 30 8






 
";
        private static System.IO.StringReader Sr = null;
        public static string ReadLine()
        {
            if (IsDebug)
            {
                if (Sr == null)
                {
                    Sr = new System.IO.StringReader(PlainInput.Trim());
                }
                return Sr.ReadLine();
            }
            else
            {
                return Console.ReadLine();
            }
        }

    }
    static void Main()
    {
        Program prg = new Program();
        prg.Proc();
    }
}
0