結果

問題 No.527 ナップサック容量問題
ユーザー 👑 tatt61880tatt61880
提出日時 2021-04-15 23:19:50
言語 Kuin
(KuinC++ v.2021.9.17)
結果
AC  
実行時間 271 ms / 2,000 ms
コード長 1,359 bytes
コンパイル時間 2,456 ms
コンパイル使用メモリ 151,228 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-10-14 18:26:29
合計ジャッジ時間 5,416 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,356 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 52 ms
4,352 KB
testcase_06 AC 248 ms
4,384 KB
testcase_07 AC 54 ms
4,352 KB
testcase_08 AC 33 ms
4,352 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 187 ms
4,348 KB
testcase_11 AC 95 ms
4,352 KB
testcase_12 AC 55 ms
4,352 KB
testcase_13 AC 271 ms
4,348 KB
testcase_14 AC 23 ms
4,352 KB
testcase_15 AC 28 ms
4,352 KB
testcase_16 AC 3 ms
4,352 KB
testcase_17 AC 42 ms
4,352 KB
testcase_18 AC 62 ms
4,352 KB
testcase_19 AC 3 ms
4,348 KB
testcase_20 AC 5 ms
4,348 KB
testcase_21 AC 30 ms
4,348 KB
testcase_22 AC 16 ms
4,348 KB
testcase_23 AC 25 ms
4,352 KB
testcase_24 AC 3 ms
4,352 KB
testcase_25 AC 13 ms
4,356 KB
testcase_26 AC 11 ms
4,352 KB
testcase_27 AC 12 ms
4,348 KB
testcase_28 AC 10 ms
4,348 KB
testcase_29 AC 30 ms
4,352 KB
testcase_30 AC 2 ms
4,356 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 2 ms
4,352 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 2 ms
4,372 KB
testcase_35 AC 74 ms
4,352 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 14 ms
4,348 KB
testcase_38 AC 32 ms
4,348 KB
testcase_39 AC 58 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

func main()
	var n: int :: cui@inputInt()
	var v: []int :: #[n]int
	var w: []int :: #[n]int
	var vSum: int :: 0
	var wSum: int :: 0
	for i(0, n - 1)
		do v[i] :: cui@inputInt()
		do w[i] :: cui@inputInt()
		do vSum :+ v[i]
		do wSum :+ w[i]
	end for
	var val: int :: cui@inputInt()
	
	var ansMin: int
	var ansMax: int
	block
		var ok: int :: 0
		var ng: int :: wSum + 1
		while((ok - ng).abs() > 1)
			var mid: int :: (ok + ng) / 2
			var res: int :: f(n, v, w, mid)
			if(res < val)
				do ok :: mid
			else
				do ng :: mid
			end if
		end while
		do ansMin :: ok + 1
	end block
	block
		var ok: int :: 0
		var ng: int :: wSum + 1
		while((ok - ng).abs() > 1)
			var mid: int :: (ok + ng) / 2
			if(f(n, v, w, mid) <= val)
				do ok :: mid
			else
				do ng :: mid
			end if
		end while
		do ansMax :: ok
	end block
	do cui@print("\{ansMin}\n")
	do cui@print("\{vSum = val ?("inf", "\{ansMax}")}\n")
	
	func f(n: int, v: []int, w: []int, weight: int): int
		; dp[i] = v
		; i: 重さの和
		; v: 価値の最大値
		var dp: []int :: #[weight + 1]int
		do dp[0] :: 1
		for j(0, n - 1)
			for i(weight, 0, -1)
				if(dp[i] <> 0 & i + w[j] <= weight)
					do dp[i + w[j]] :: lib@max(dp[i + w[j]], dp[i] + v[j])
				end if
			end for
		end for
		var res: int :: 0
		for i(0, weight)
			do res :: lib@max(res, dp[i])
		end for
		ret res - 1
	end func
end func
0