結果

問題 No.527 ナップサック容量問題
ユーザー mban
提出日時 2017-06-10 22:53:10
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 53 ms / 2,000 ms
コード長 1,629 bytes
コンパイル時間 897 ms
コンパイル使用メモリ 112,572 KB
実行使用メモリ 27,492 KB
最終ジャッジ日時 2024-09-24 15:47:07
合計ジャッジ時間 3,494 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.

ソースコード

diff #

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;

class Program
{
    static void Main()
    {
        new Magatro().Solve();
    }
}

class Magatro
{
    private int N;
    private int[] v, w;
    private int V;
    const int Max = 100000;
    private void Scan()
    {
        N = int.Parse(Console.ReadLine());
        v = new int[N];
        w = new int[N];
        for (int i = 0; i < N; i++)
        {
            var line = Console.ReadLine().Split(' ');
            v[i] = int.Parse(line[0]);
            w[i] = int.Parse(line[1]);
        }
        V = int.Parse(Console.ReadLine());
    }

    public void Solve()
    {
        Scan();
        var dp = (new int[Max + 1]).Select(i => -1).ToArray();
        dp[0] = 0;
        for (int i = 0; i < N; i++)
        {
            for (int j = Max; j >= 0; j--)
            {
                if (dp[j] != -1)
                {
                    dp[j + w[i]] = Math.Max(dp[j + w[i]], dp[j] + v[i]);
                }
            }
        }
        int min = -1;
        int max = -1;
        for (int i = 0; i <= Max; i++)
        {
            if (dp[i] == V && min == -1)
            {
                min = i;
            }
            if(dp[i]>V&&max == -1)
            {
                max = i - 1;
            }
        }


        Console.WriteLine(Math.Max(1,min));
        if (max == -1)
        {
            Console.WriteLine("inf");
        }
        else
        {
            Console.WriteLine(max);
        }
    }
}
0