gets b=u=[] gets.split.map{|q|v=q.to_i;s=b.size;i=b.index{|k|k>v}||s;u+=[*-s...-i];b[i..-1]=v;} p u.size u.map{|i|puts [~i,-i]*' '}