import Control.Applicative ((<$>), (<*>)) import Data.ByteString.Char8 (ByteString) import qualified Data.ByteString.Char8 as B import System.IO (hFlush, stdout) import Data.List (foldl') import Data.Char (isSpace) import Data.Vector (Vector, (!), (//)) import qualified Data.Vector as V main :: IO () main = do n <- readi B.readInt <$> B.getLine solve n <$> readiv B.readInt <$> B.getLine >>= mapM putStrLn hFlush stdout B.getLine return () solve :: Int -> Vector Int -> [String] solve n v = show (length rs) : (reverse rs) where ids = [(j, j+1) | i <- [n-2, n-3 .. 0], j <- [0 .. i]] f (tv, rs) (i, j) | (tv ! i) <= (tv ! j) = (tv, rs) | otherwise = (tv // [(i, tv ! j), (j, tv ! i)], unwords [show i, show j] : rs) (_, rs) = foldl' f (v, []) ids readi :: Integral a => (ByteString -> Maybe (a, ByteString)) -> ByteString -> a readi f s = let Just (n, _) = f s in n readiv :: Integral a => (ByteString -> Maybe (a, ByteString)) -> ByteString -> Vector a readiv f = V.unfoldr g where g s = do (n, s') <- f s return (n, B.dropWhile isSpace s')