結果

問題 No.397 NO MORE KADOMATSU
コンテスト
ユーザー ducktail
提出日時 2018-07-06 15:16:55
言語 Haskell
(9.14.1)
コンパイル:
ghc -rtsopts -with-rtsopts=-K1G -o a.out -O2 _filename_
実行:
./a.out
結果
MLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,128 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 9,715 ms
コンパイル使用メモリ 232,892 KB
実行使用メモリ 261,240 KB
平均クエリ数 936.56
最終ジャッジ日時 2026-03-31 16:25:05
合計ジャッジ時間 5,432 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 17 MLE * 1
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.14.1/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
[2 of 2] Linking a.out

ソースコード

diff #
raw source code

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')
0