結果

問題 No.50 おもちゃ箱
ユーザー 14番14番
提出日時 2016-06-13 03:35:07
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,352 ms / 5,000 ms
コード長 5,179 bytes
コンパイル時間 2,421 ms
コンパイル使用メモリ 109,452 KB
実行使用メモリ 48,004 KB
最終ジャッジ日時 2023-09-08 03:53:01
合計ジャッジ時間 14,596 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
28,000 KB
testcase_01 AC 470 ms
46,204 KB
testcase_02 AC 85 ms
28,104 KB
testcase_03 AC 75 ms
23,928 KB
testcase_04 AC 75 ms
21,920 KB
testcase_05 AC 1,352 ms
40,344 KB
testcase_06 AC 195 ms
34,824 KB
testcase_07 AC 76 ms
21,764 KB
testcase_08 AC 77 ms
21,888 KB
testcase_09 AC 78 ms
21,760 KB
testcase_10 AC 75 ms
19,904 KB
testcase_11 AC 81 ms
23,904 KB
testcase_12 AC 78 ms
23,928 KB
testcase_13 AC 76 ms
21,852 KB
testcase_14 AC 85 ms
23,720 KB
testcase_15 AC 78 ms
21,832 KB
testcase_16 AC 85 ms
23,900 KB
testcase_17 AC 150 ms
33,348 KB
testcase_18 AC 97 ms
28,784 KB
testcase_19 AC 411 ms
46,184 KB
testcase_20 AC 91 ms
26,588 KB
testcase_21 AC 169 ms
31,220 KB
testcase_22 AC 376 ms
42,636 KB
testcase_23 AC 76 ms
21,752 KB
testcase_24 AC 75 ms
23,928 KB
testcase_25 AC 73 ms
21,968 KB
testcase_26 AC 92 ms
26,120 KB
testcase_27 AC 253 ms
36,104 KB
testcase_28 AC 374 ms
40,668 KB
testcase_29 AC 379 ms
42,572 KB
testcase_30 AC 158 ms
35,364 KB
testcase_31 AC 79 ms
21,888 KB
testcase_32 AC 437 ms
44,316 KB
testcase_33 AC 413 ms
48,004 KB
testcase_34 AC 1,017 ms
47,476 KB
testcase_35 AC 479 ms
42,932 KB
testcase_36 AC 375 ms
42,444 KB
testcase_37 AC 385 ms
43,304 KB
testcase_38 AC 280 ms
42,048 KB
testcase_39 AC 568 ms
43,372 KB
testcase_40 AC 405 ms
42,376 KB
testcase_41 AC 186 ms
32,252 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 toyCount = int.Parse(Reader.ReadLine());
        this.ToyList = Reader.ReadLine().Split(' ').Select(a=>int.Parse(a)).ToArray();

        int boxCount = int.Parse(Reader.ReadLine());
        this.BoxList = Reader.ReadLine().Split(' ').Select(a=>int.Parse(a)).ToArray();

        BoxCountResult ans = this.CountBox(0, 0);
        Console.WriteLine(ans.Count);
    }

    private int[] ToyList;
    private int[] BoxList;

    public struct BoxCountResult {
        public int target;
        public int Count;
    }

    public struct ToyCountResult {
        public int target;
        public int Remain;
    }

    private Dictionary<int, Dictionary<int, BoxCountResult>> BoxDic = new Dictionary<int, Dictionary<int, BoxCountResult>>();
    private BoxCountResult CountBox(int idx, int target){
        if(!this.BoxDic.ContainsKey(idx)) {
            this.BoxDic.Add(idx, new Dictionary<int, BoxCountResult>());
        }
        if(this.BoxDic[idx].ContainsKey(target)) {
            return this.BoxDic[idx][target];
        }

        BoxCountResult ans;
        ans.target = target;
        ans.Count = -1;

        int allUsed = (1<<this.ToyList.Length)-1;
        if(target == allUsed) {
            ans.Count = 0;
            return ans;
        }

        if(idx == this.BoxList.Length) {
            ans.Count = -1;
            return ans;
        }

        BoxCountResult ret1;
        ret1.target = target;
        ret1.Count = -1;
        List<ToyCountResult> tr = this.CountOmocha(0, target, this.BoxList[idx]);
        for(int i=0; i<tr.Count; i++) {
            ToyCountResult a = tr[i];
            if(a.target != target) {
                if(idx < this.BoxList.Length - 1) {
                    BoxCountResult tmp = this.CountBox(idx+1, a.target);
                    if(tmp.Count >= 0) {
                        tmp.Count+=1;
                        if(ret1.Count < 0 || ret1.Count > tmp.Count) {
                            ret1 = tmp;
                        }
                    }
                } else if(a.target == allUsed) {
                    ret1.Count = 1;
                    ret1.target = a.target;
                    break;
                }
            }
        }

        BoxCountResult ret2 = this.CountBox(idx+1, target);

        if(ret1.Count >= 0 && ret2.Count >= 0) {
            if(ret1.Count < ret2.Count) {
                ans = ret1;
            } else {
                ans = ret2;
            }
        } else if(ret1.Count >= 0) {
            ans = ret1;
        } else if(ret2.Count >=0 ) {
            ans = ret2;
        }
        this.BoxDic[idx].Add(target, ans);
        return ans;
    }

    Dictionary<int, Dictionary<int, int>> LargeistDic = new Dictionary<int, Dictionary<int, int>>();

    private Dictionary<int, Dictionary<int, Dictionary<int, List<ToyCountResult>>>> ToyDic = new Dictionary<int, Dictionary<int, Dictionary<int, List<ToyCountResult>>>>();
    private List<ToyCountResult> CountOmocha(int idx, int target, int remain) {
        if(!this.ToyDic.ContainsKey(idx)) {
            this.ToyDic.Add(idx, new Dictionary<int, Dictionary<int, List<ToyCountResult>>>());
        }
        if(!this.ToyDic[idx].ContainsKey(target)) {
            this.ToyDic[idx].Add(target, new Dictionary<int, List<ToyCountResult>>());
        }
        if(this.ToyDic[idx][target].ContainsKey(remain)) {
            return this.ToyDic[idx][target][remain];
        }

        List<ToyCountResult> ans = new List<ToyCountResult>();
        ToyCountResult tmp;
        tmp.target = target;
        tmp.Remain = remain;
        ans.Add(tmp);


        int allUsed = (1<<this.ToyList.Length) - 1;
        if(target == allUsed) {
            return ans;
        }

        if(idx == this.ToyList.Length) {
            return ans;
        }

        List<ToyCountResult> ret1 = new List<ToyCountResult>();
        if(this.ToyList[idx] <= remain && (target & (1<<idx)) == 0) {
            ret1 = this.CountOmocha(idx+1, target + (1<<idx), remain - this.ToyList[idx]);
        }
        List<ToyCountResult> ret2 = this.CountOmocha(idx+1, target, remain);

        ret1.Union(ret2).Distinct().ToList().ForEach(a=>{
            if(!ans.Contains(a)) {
                ans.Add(a);
            }
        });


        this.ToyDic[idx][target].Add(remain, ans);
        return ans;
    }

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


2
2 3
1
5



";
        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