diff --git a/solutions/ruby/median_sort.rb b/solutions/ruby/median_sort.rb new file mode 100644 index 00000000..a595623a --- /dev/null +++ b/solutions/ruby/median_sort.rb @@ -0,0 +1,79 @@ +$stdout.sync = true + +def valid res + if res == -1 + exit 0 + end + return res +end + +def printsAnswer res + puts res.join(' ') + $stdout.flush + judge = STDIN.gets.chomp.to_i; + valid(judge) +end + +def query(i, j, k) + puts "#{i} #{j} #{k}" + $stdout.flush + + res = STDIN.gets.chomp.to_i + + return valid(res) +end + +def solve ans + res = [1, 2] + n = ans.size + + # try new index + for number in 3.upto(n) + start = 0 + finish = res.size - 1 + + + while finish-start >= 1 + + # divide list in 3 parts + a = start + (finish - start) / 3 + b = finish - (finish - start) / 3 + + # query median + median = query(res[a], res[b], number) + + # move limits of parts according to median + if median == res[a] + finish = a - 1 + if start == finish + finish += 1 + end + elsif median == res[b] + start = b + 1 + if start == finish + start -= 1 + end + elsif median == number + start = a + 1 + finish = b - 1 + if start == finish + start -= 1 + end + end + end + # insert at correct index + res.insert(start, number) + end + + + printsAnswer(res) +end + +t, n, q = STDIN.gets.split.map &:to_i; + +0.upto(t - 1) do + answer = Array(1..n) + solve(answer) +end + +exit 0 diff --git a/solutions/ruby/moons_and_umbrellas.rb b/solutions/ruby/moons_and_umbrellas.rb new file mode 100644 index 00000000..711252ff --- /dev/null +++ b/solutions/ruby/moons_and_umbrellas.rb @@ -0,0 +1,38 @@ +# Read the number of test cases. +t = gets.to_i + +def solve + + #get string of C and J and ? + a, b, str = gets.split(" ") + + # get costs + x = a.to_i + y = b.to_i + + # with dynamic programming + cCost = (str[0] == 'J' ? 1e9 : 0) + jCost = (str[0] == 'C' ? 1e9 : 0) + + for i in 1 ... str.size + cCostNew = 1e9 + jCostNew = 1e9 + if str[i] == 'C' + cCostNew = [cCost, jCost + y].min + elsif str[i] == 'J' + jCostNew = [jCost, cCost + x].min + else + cCostNew = [cCost, jCost + y].min + jCostNew = [jCost, cCost + x].min + end + cCost = cCostNew + jCost = jCostNew + end + + return [cCost, jCost].min +end + +# Loop over the number of test cases. +1.upto(t) do |ti| + puts "Case ##{ti}: #{solve}" +end diff --git a/solutions/ruby/parenting_partnering_returns.rb b/solutions/ruby/parenting_partnering_returns.rb new file mode 100644 index 00000000..94a539d5 --- /dev/null +++ b/solutions/ruby/parenting_partnering_returns.rb @@ -0,0 +1,47 @@ +# Read the number of test cases. +t = gets.to_i + +def solve + + # get number of activities + n = gets.to_i + activities = Array.new() + + # get activities and order them by start time + 0.upto(n-1) do |id| + start, finish = gets.split(" ").map(&:to_i) + activities.push([start, finish, id]) + end + + # sort by ascending order of start time + activities.sort_by! {|e| e[0]} + + c = 0 + j = 0 + strAnswer = "x" * n + + # assigns activity one by one if possible, otherwise return + activities.each do |activity| + start = activity[0] + finish = activity[1] + index = activity[2] + + if c <= start + c = finish + strAnswer[index] = "C" + elsif j <= start + j = finish + strAnswer[index] = "J" + else + strAnswer = "IMPOSSIBLE" + break + end + end + + return "#{strAnswer}" +end + +# Loop over the number of test cases. +1.upto(t) do |ti| + puts "Case ##{ti}: #{solve}" +end diff --git a/solutions/ruby/reversort_engineering.rb b/solutions/ruby/reversort_engineering.rb new file mode 100644 index 00000000..8078748a --- /dev/null +++ b/solutions/ruby/reversort_engineering.rb @@ -0,0 +1,41 @@ +# Read the number of test cases. +t = gets.to_i + +def solve + + #get string n and c + n, c = gets.split(" ").map(&:to_i) + + # cost until the last element + c -= n - 1 + + costsArr = Array.new() + + # is it possible? + for cost in (n - 1).downto(1) + if c >= cost + costsArr.push(cost) + c -= cost + end + end + + if c != 0 + return "IMPOSSIBLE" + end + + ans = Array.new() + for i in 1.upto(n) + ans.push(i) + end + + #there's n - 2 numbers to go + for i in (costsArr.size - 1).downto(0) + ans[n-1-costsArr[i], n] = ans[n-1-costsArr[i], n].reverse + end + return ans.join(" ") +end + +# Loop over the number of test cases. +1.upto(t) do |ti| + puts "Case ##{ti}: #{solve}" +end