-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmain.rb
More file actions
73 lines (64 loc) · 1.18 KB
/
main.rb
File metadata and controls
73 lines (64 loc) · 1.18 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
THRESHOLD = 20
class Point
attr_accessor :i, :x, :y
end
def find_ranks ps
if ps.size <= THRESHOLD
(1...ps.size).each do |i|
tp = ps.delete_at(i)
if tp.y < ps[0].y
ps.unshift(tp)
next
end
j = i
loop do
break if tp.y >= ps[j - 1].y
j -= 1
end
ps.insert(j, tp)
$ranks[tp.i] += j
end
return ps
end
mid = ps.size / 2
left = find_ranks(ps[0...mid])
right = find_ranks(ps[mid..-1])
ps = []
count = 0
loop do
if left.first.y <= right.first.y
ps << left.shift
count += 1
if left.empty?
right.each do |p|
$ranks[p.i] += count
end
ps += right
break
end
else
$ranks[right.first.i] += count
ps << right.shift
if right.empty?
ps += left
break
end
end
end
return ps
end
loop do
n = gets.chomp.to_i
break if n == 0
ps = Array.new(n) { Point.new }
$ranks = [0] * n
n.times do |i|
ps[i].i, ps[i].x, ps[i].y = [i] + gets.chomp.split.map(&:to_i)
end
ps.sort! do |a, b|
next a.y <=> b.y if a.x == b.x
a.x <=> b.x
end
find_ranks(ps)
puts $ranks.join(' ')
end