結果

問題 No.490 yukiソート
コンテスト
ユーザー ducktail
提出日時 2018-04-11 16:43:49
言語 Haskell
(9.14.1)
コンパイル:
ghc -rtsopts -with-rtsopts=-K1G -o a.out -O2 _filename_
実行:
./a.out
結果
AC  
実行時間 16 ms / 2,000 ms
コード長 1,295 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,728 ms
コンパイル使用メモリ 231,892 KB
実行使用メモリ 8,448 KB
最終ジャッジ日時 2026-03-13 16:42:40
合計ジャッジ時間 4,723 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 34
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 Control.Monad (forM_, when)

import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as B

import Data.Char (isSpace)

import Data.Vector (Vector, (!))
import qualified Data.Vector as V
import Data.Vector.Mutable (IOVector)
import qualified Data.Vector.Mutable as VM

main :: IO ()
main = do
  n <- readLn
  v <- getl (readiv B.readInt)
  solve n v

solve :: Int -> Vector Int -> IO ()
solve n v = do
  mv <- V.thaw v
  yukisort n mv
  printVector n mv

getl :: (ByteString -> a) -> IO a
getl f = f <$> B.getLine

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

yukisort :: (Ord a, Eq a) => Int -> IOVector a -> IO ()
yukisort n mv = do
  forM_ [1 .. 2*n-4] $ \i -> do
    forM_ [0 .. i `div` 2] $ \p -> do
      let q = i - p
      when (p < q && q <= n - 1) $ do
        pa <- VM.read mv p
        qa <- VM.read mv q
        when (pa > qa) $ do
          VM.swap mv p q

printVector :: Show a => Int -> IOVector a -> IO ()
printVector n mv = do
  forM_ [0..n-1] $ \i -> do
    x <- VM.read mv i
    if i /= 0 then putStr (" " ++ (show x))
    else putStr (show x)
  putStrLn ""
0