Created
April 5, 2011 15:18
-
-
Save pcomans/903814 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
f = File.open("A-large-practice.in") | |
num_testcases = f.readline.strip.to_i | |
casenumber = 1 | |
num_testcases.times do | |
credit = f.readline.strip.to_i | |
num_items = f.readline.strip.to_i | |
prices = f.readline.split(' ').collect {|x| x.to_i} | |
#Write positions to a Hash because we want to sort prices and need to remember the position | |
positions = {} | |
(1..num_items).each do |pos| | |
positions[pos] = prices[pos-1] | |
end | |
#Sort prices in a descending manner. | |
sorted_prices = prices.sort.reverse | |
solved = false | |
while !solved | |
first_number = sorted_prices[0] | |
sorted_prices = sorted_prices[1, num_items-1] | |
sorted_prices.each do |second_number| | |
if first_number + second_number == credit | |
solved = true | |
indices = [] | |
first_position = positions.key(first_number) | |
indices.push first_position | |
positions.delete(first_position) | |
second_position = positions.key(second_number) | |
indices.push second_position | |
indices.sort! | |
print "Case #" + casenumber.to_s + ": " | |
indices.each { |i| print i.to_s + " "} | |
print "\n" | |
break | |
end | |
end | |
end | |
casenumber = casenumber + 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment