結果

問題 No.527 ナップサック容量問題
ユーザー aketijyuuzou
提出日時 2024-10-10 23:49:44
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 920 ms / 2,000 ms
コード長 3,167 bytes
コンパイル時間 1,395 ms
コンパイル使用メモリ 114,492 KB
実行使用メモリ 37,176 KB
最終ジャッジ日時 2024-10-10 23:49:55
合計ジャッジ時間 9,833 ms
ジャッジサーバーID
(参考情報)
judge1 / 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.

ソースコード

diff #

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
            //荷物がふたつとも入ってしまったので、
            //容量は518以上である。ということしかわかりませんでした。
        }
        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;
            }
        }

        //最小のナップサックの重さを求める
        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");
        }
    }
}

0