Skip to content

Instantly share code, notes, and snippets.

@fukayatsu
Last active December 22, 2015 04:58
Show Gist options
  • Save fukayatsu/6420247 to your computer and use it in GitHub Desktop.
Save fukayatsu/6420247 to your computer and use it in GitHub Desktop.
某クロッシング問題をredisで解いてみた: ( https://codeiq.jp/ace/yuki_hiroshi/q432 )
#!/usr/env/ruby -v
require 'benchmark'
measure = Benchmark.measure {
require 'redis'
redis = Redis.new
redis.del :ray
INPUT = 'crossing.txt'
rays = []
open(INPUT).read.each_line { |line|
rays << line.chomp.to_i - 1
}
p rays.count
puts
crossing_count = 0
rays.each.with_index do |ray, i|
redis.zadd :ray, ray, ray
crossing_count += redis.zcount(:ray, ray + 1, '+inf')
puts "#{i}, #{Time.now}" if i % 10000 == 0
end
puts
p crossing_count
}
puts Benchmark::CAPTION
puts measure #=> 45秒ぐらい
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment