結果

問題 No.527 ナップサック容量問題
ユーザー kuuso1kuuso1
提出日時 2017-06-09 23:11:37
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 604 ms / 2,000 ms
コード長 1,920 bytes
コンパイル時間 2,584 ms
コンパイル使用メモリ 112,088 KB
実行使用メモリ 28,956 KB
最終ジャッジ日時 2023-10-24 01:08:21
合計ジャッジ時間 8,556 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
24,080 KB
testcase_01 AC 25 ms
24,080 KB
testcase_02 AC 25 ms
24,080 KB
testcase_03 AC 25 ms
24,080 KB
testcase_04 AC 26 ms
24,080 KB
testcase_05 AC 131 ms
25,428 KB
testcase_06 AC 555 ms
28,724 KB
testcase_07 AC 133 ms
24,300 KB
testcase_08 AC 92 ms
24,828 KB
testcase_09 AC 26 ms
24,080 KB
testcase_10 AC 421 ms
26,684 KB
testcase_11 AC 225 ms
26,016 KB
testcase_12 AC 142 ms
25,536 KB
testcase_13 AC 604 ms
28,956 KB
testcase_14 AC 69 ms
24,320 KB
testcase_15 AC 78 ms
24,084 KB
testcase_16 AC 26 ms
24,080 KB
testcase_17 AC 110 ms
24,800 KB
testcase_18 AC 152 ms
25,320 KB
testcase_19 AC 26 ms
24,080 KB
testcase_20 AC 31 ms
24,080 KB
testcase_21 AC 83 ms
24,084 KB
testcase_22 AC 52 ms
24,084 KB
testcase_23 AC 72 ms
24,084 KB
testcase_24 AC 28 ms
24,080 KB
testcase_25 AC 47 ms
24,084 KB
testcase_26 AC 43 ms
24,084 KB
testcase_27 AC 45 ms
24,084 KB
testcase_28 AC 41 ms
24,084 KB
testcase_29 AC 82 ms
24,084 KB
testcase_30 AC 26 ms
24,080 KB
testcase_31 AC 26 ms
24,080 KB
testcase_32 AC 27 ms
24,084 KB
testcase_33 AC 26 ms
24,084 KB
testcase_34 AC 26 ms
24,084 KB
testcase_35 AC 177 ms
25,276 KB
testcase_36 AC 26 ms
24,080 KB
testcase_37 AC 51 ms
24,080 KB
testcase_38 AC 94 ms
24,312 KB
testcase_39 AC 140 ms
25,068 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.Linq;
using System.Text;

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){
		
		int tot = 0;
		for(int i=0;i<N;i++) tot += W[i];
		
		int l = 1, r = tot+1;
		while(r - l > 1){
			int c = (r + l) / 2;
			int v = Knap(c);
			if(v < V0){
				l = c;
			} else {
				r = c;
			}
		}
		int min = r;
		if(Knap(l) == V0) min = l;
		
		l = 1; r = tot+1;
		while(r - l > 1){
			int c = (r + l) / 2;
			int v = Knap(c);
			if(v <= V0){
				l = c;
			} else {
				r = c;
			}
		}
		int max = l;
		
		Console.WriteLine(min);
		Console.WriteLine(max == tot ? "inf" : max.ToString());
		
	}
	
	int Knap(int weight){
		
		int[] dp = new int[weight+1];
		for(int i=1;i<=weight;i++) dp[i] = -1;
		dp[0] = 0;
		for(int i=0;i<N;i++){
			for(int j=weight;j>=0;j--){
				if(dp[j] == -1 || j + W[i] > weight) continue;
				dp[j + W[i]] = Math.Max(dp[j + W[i]], dp[j] + V[i]);
			}
		}
		
		int ret = 0;
		for(int i=0;i<=weight;i++) ret = Math.Max(ret,dp[i]);
		return ret;
		
	}
	
	
	
	int N;
	int[] V,W;
	int V0;
	public Sol(){
		N = ri();
		V = new int[N];
		W = new int[N];
		for(int i=0;i<N;i++){
			var d = ria();
			V[i] = d[0]; W[i] = d[1];
		}
		V0 = ri();
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}
0