結果

問題 No.50 おもちゃ箱
ユーザー 14番14番
提出日時 2016-06-13 03:35:07
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,381 ms / 5,000 ms
コード長 5,179 bytes
コンパイル時間 1,064 ms
コンパイル使用メモリ 112,692 KB
実行使用メモリ 43,520 KB
最終ジャッジ日時 2024-06-25 21:05:28
合計ジャッジ時間 11,843 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
23,856 KB
testcase_01 AC 467 ms
41,600 KB
testcase_02 AC 54 ms
23,988 KB
testcase_03 AC 44 ms
19,584 KB
testcase_04 AC 43 ms
19,456 KB
testcase_05 AC 1,381 ms
38,272 KB
testcase_06 AC 186 ms
32,512 KB
testcase_07 AC 46 ms
19,712 KB
testcase_08 AC 47 ms
20,352 KB
testcase_09 AC 46 ms
19,840 KB
testcase_10 AC 44 ms
19,712 KB
testcase_11 AC 48 ms
20,992 KB
testcase_12 AC 46 ms
20,224 KB
testcase_13 AC 46 ms
19,968 KB
testcase_14 AC 51 ms
22,272 KB
testcase_15 AC 44 ms
19,840 KB
testcase_16 AC 52 ms
23,168 KB
testcase_17 AC 125 ms
28,672 KB
testcase_18 AC 66 ms
24,064 KB
testcase_19 AC 403 ms
41,600 KB
testcase_20 AC 64 ms
23,808 KB
testcase_21 AC 143 ms
28,928 KB
testcase_22 AC 362 ms
39,808 KB
testcase_23 AC 44 ms
19,712 KB
testcase_24 AC 43 ms
19,840 KB
testcase_25 AC 44 ms
19,584 KB
testcase_26 AC 60 ms
23,596 KB
testcase_27 AC 223 ms
31,104 KB
testcase_28 AC 342 ms
38,144 KB
testcase_29 AC 335 ms
38,016 KB
testcase_30 AC 126 ms
30,720 KB
testcase_31 AC 46 ms
20,224 KB
testcase_32 AC 412 ms
42,112 KB
testcase_33 AC 397 ms
43,520 KB
testcase_34 AC 1,004 ms
41,216 KB
testcase_35 AC 468 ms
40,320 KB
testcase_36 AC 366 ms
39,936 KB
testcase_37 AC 359 ms
38,784 KB
testcase_38 AC 252 ms
37,504 KB
testcase_39 AC 531 ms
40,832 KB
testcase_40 AC 378 ms
38,144 KB
testcase_41 AC 154 ms
29,568 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