Last active
December 30, 2015 02:29
-
-
Save parrot-studio/7763295 to your computer and use it in GitHub Desktop.
https://paiza.jp/poh/ec-campaign に挑戦したコード
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
# coding: utf-8 | |
def read_data(count) | |
c = count.to_i | |
return if c < 0 | |
ret = [] | |
count.times do | |
s = gets | |
next unless s | |
ret << s.to_i | |
end | |
ret | |
end | |
def max_price(target, prices) | |
return if target < 0 | |
return if (prices.nil? || prices.empty?) | |
return 0 if target <= 0 | |
ps = prices.reject{|pr| pr > target} | |
return 0 if ps.empty? | |
max = 0 | |
ps.combination(2).each do |d| | |
pr = d.first + d.last | |
next if pr > target | |
max = pr if pr > max | |
break if max == target | |
end | |
max | |
end | |
counts = gets | |
targets = counts.chomp.split(' ') | |
pcount = targets.first.to_i | |
dcount = targets.last.to_i | |
exit if (pcount < 0 || dcount < 0) | |
prices = read_data(pcount) | |
targets = read_data(dcount) | |
targets.each do |target| | |
rsl = max_price(target, prices) | |
puts (rsl ? rsl : 0) | |
end | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
28a3e75の結果
http://paiza.jp/poh/ec-campaign/result/1da1af4191a0f4883f5a4c43ebaa2091