Created
January 5, 2019 14:59
-
-
Save tomcha/db0f9acd1b590eaa0c0bc2b1aa45e247 to your computer and use it in GitHub Desktop.
動的計画法を使った部分和(解説を見て)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
n = gets.chomp.to_i | |
a = gets.chomp.split(/ /).map(&:to_i) | |
ans = gets.chomp.to_i | |
dp = [] | |
(0..n).each do |i| | |
dp << [] | |
(0..ans).each do |a| | |
dp[i][a] = false | |
end | |
end | |
dp[0][0] = true | |
#dp[i番目までのいくつか][j]になるか | |
(0..(n - 1)).each do |i| | |
(0..ans).each do |aa| | |
dp[i + 1][aa] = dp[i][aa] | |
if(aa >= a[i]) | |
dp[i + 1][aa] ||= dp[i][aa - a[i]] | |
end | |
end | |
end | |
puts dp[n][ans] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment