結果
問題 |
No.527 ナップサック容量問題
|
ユーザー |
![]() |
提出日時 | 2017-06-10 05:05:29 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 910 ms / 2,000 ms |
コード長 | 3,047 bytes |
コンパイル時間 | 1,162 ms |
コンパイル使用メモリ | 118,744 KB |
実行使用メモリ | 48,096 KB |
最終ジャッジ日時 | 2024-09-23 02:31:35 |
合計ジャッジ時間 | 9,772 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 37 |
コンパイルメッセージ
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.Linq; class Program { static string InputPattern = "InputX"; static List<string> GetInputList() { var WillReturn = new List<string>(); if (InputPattern == "Input1") { WillReturn.Add("3"); WillReturn.Add("5 3"); WillReturn.Add("9 8"); WillReturn.Add("3 2"); WillReturn.Add("8"); //5 //7 } else if (InputPattern == "Input2") { WillReturn.Add("4"); WillReturn.Add("33 4"); WillReturn.Add("114 514"); WillReturn.Add("123 456"); WillReturn.Add("3 14"); WillReturn.Add("0"); //1 //3 } else if (InputPattern == "Input3") { WillReturn.Add("2"); WillReturn.Add("33 4"); WillReturn.Add("114 514"); WillReturn.Add("147"); //518 //inf } else { string wkStr; while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr); } return WillReturn; } struct NimotuDef { internal int V; internal int W; } static void Main() { List<string> InputList = GetInputList(); int[] wkArr = { }; Action<string> SplitAct = pStr => wkArr = pStr.Split(' ').Select(X => int.Parse(X)).ToArray(); int N = int.Parse(InputList[0]); NimotuDef[] NimotuArr = new NimotuDef[N]; for (int I = 1; I <= N; I++) { SplitAct(InputList[I]); NimotuArr[I - 1].V = wkArr[0]; NimotuArr[I - 1].W = wkArr[1]; } int V = int.Parse(InputList[N + 1]); //最大の価値合計[重さ合計]なDP表 var DPDict = new Dictionary<int, int>(); DPDict[0] = 0; foreach (NimotuDef EachNimotu in NimotuArr) { foreach (int EachKey in DPDict.Keys.OrderByDescending(X => X).ToArray()) { //価値超過を枝切り if (DPDict[EachKey] > V) continue; int NewW = EachKey + EachNimotu.W; int NewV = DPDict[EachKey] + EachNimotu.V; if (DPDict.ContainsKey(NewW) && DPDict[NewW] > NewV) continue; DPDict[NewW] = NewV; } } //foreach (var EachPair in DPDict.OrderBy(X => X.Key)) { // Console.WriteLine("DPDict[{0}]={1}", EachPair.Key, EachPair.Value); //} //最小のナップサックの重さを求める int MinW = DPDict.Where(X => X.Value == V).Min(X => X.Key); if (MinW == 0) MinW = 1; Console.WriteLine(MinW); //最大のナップサックの重さを求める var Tmp = DPDict.Where(X => X.Value > V); if (Tmp.Any()) { Console.WriteLine(Tmp.Min(X => X.Key) - 1); } else { Console.WriteLine("inf"); } } }