結果
問題 | No.50 おもちゃ箱 |
ユーザー |
![]() |
提出日時 | 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 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 38 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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(); } }