Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

SPOJ:SMCNT

Name: NULL 2012-03-15 14:24

Hi /prog/,
I'm trying to solve a challenge a friend gave me.
I have a huge list A[] (10^6 in length) of numbers, and for each k i have to print the number of A[k'] with: k' > k and A[k'] < A[k].
Example:
In: 12 5 7 3 0 1
Out: 5 3 3 2 0 0

I've thought about using a research binary tree or a red-black tree. Thoughts ?
(excuse my English, it's not my natural tongue.)

Name: Anonymous 2012-03-15 14:51

I don't know how far a simplistic naïve approach would get you, but why not try something like...

nums = gets(nil).split.map { |x| x.to_i }
counts = Array.new(nums.size, 0)

nums.each_with_index do |n, i|
  i.times do |j|
    counts[j] += 1 if n < nums[j]
  end
end

puts counts.join(' ')

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List