結果

問題 No.527 ナップサック容量問題
ユーザー さかぽん
提出日時 2021-05-18 10:50:23
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 832 bytes
コンパイル時間 999 ms
コンパイル使用メモリ 112,780 KB
実行使用メモリ 26,172 KB
最終ジャッジ日時 2024-10-08 07:34:22
合計ジャッジ時間 3,854 ms
ジャッジサーバーID
(参考情報)
judge3 / 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;

class C
{
	static int[] Read() => Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
	static (int, int) Read2() { var a = Read(); return (a[0], a[1]); }
	static void Main() => Console.WriteLine(Solve());
	static object Solve()
	{
		var n = int.Parse(Console.ReadLine());
		var ps = Array.ConvertAll(new bool[n], _ => Read2());
		var mv = int.Parse(Console.ReadLine());

		var mw = 1000 * n;
		var dp = Array.ConvertAll(new bool[mw + 2], _ => -1);
		dp[0] = 0;

		foreach (var (v, w) in ps)
		{
			for (int i = mw; i >= w; i--)
			{
				if (dp[i - w] == -1) continue;
				dp[i] = Math.Max(dp[i], dp[i - w] + v);
			}
		}

		var min = mv == 0 ? 1 : Array.IndexOf(dp, mv);
		var max = min;
		while (max <= mw && dp[max + 1] <= mv) max++;

		return $"{min}\n{(max == dp.Length - 1 ? "inf" : max.ToString())}";
	}
}
0