n = gets.to_i a = gets.split.map(&:to_i) gets def check x x.each_cons(3).any? { |a,b,c| next true if a < b && b > c && a != c next true if a > b && b < c && a != c next false } end ans = [] while check a u,v = -1,-1 while u == v u = rand n v = rand n end a[u], a[v] = a[v], a[u] ans << [u,v] end puts ans.size ans.each { |a,b| puts "#{a} #{b}" }