-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathparenting_partnering_returns.rb
More file actions
47 lines (38 loc) · 1012 Bytes
/
parenting_partnering_returns.rb
File metadata and controls
47 lines (38 loc) · 1012 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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