#!/bin/ruby def depth array if array.length==0 return 0 end return Math.log2(array.length).ceil end def scramble array if array.length<2 return array end mid=(array.length)/2 return [array[mid]]+scramble(array[0..(mid-1)])+scramble(array[(mid+1)..-1]) end q = gets.strip.to_i for a0 in (0..q-1) n,c = gets.strip.split(' ').map(&:to_i) sum=((n-1)*(n))/2 if c>sum puts -1 next end flag = false # your code goes here leftarray=[] rightarray=(1..n).to_a c=sum-c #puts c while c>0 if rightarray.length==0 puts -1 flag=true break end if c>=rightarray.length-1 leftarray << rightarray.delete_at(-1) c-=leftarray.length-depth(leftarray) #puts c else newidx=rightarray.length-1-c tmp=rightarray[newidx] rightarray[newidx]=rightarray[newidx-1] rightarray[newidx-1]=tmp #rightarray.insert(newidx,rightarray.delete_at(-1)) c=0 end end if flag next end #puts leftarray.length.to_s+ " "+rightarray.length.to_s puts (scramble(leftarray)+rightarray).join(" ") end #the numbers stored at a specific depth are less than 2*numbers stored at the lower depth