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...i]=v;} p u.size u.map{|i|puts [~i,-i]*' '}