Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruby solutions - Apprentice 2022D #407

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions solutions/ruby/median_sort.rb
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions solutions/ruby/moons_and_umbrellas.rb
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions solutions/ruby/parenting_partnering_returns.rb
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions solutions/ruby/reversort_engineering.rb
Original file line number Diff line number Diff line change
@@ -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