結果

問題 No.998 Four Integers
ユーザー こまるこまる
提出日時 2020-10-31 11:10:11
言語 Haskell
(9.8.2)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,703 bytes
コンパイル時間 522 ms
コンパイル使用メモリ 205,056 KB
最終ジャッジ日時 2024-11-14 23:53:52
合計ジャッジ時間 876 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.8.2/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )

Main.hs:47:31: error: [GHC-88464]
    Variable not in scope:
      fix
        :: ((v0 s0 a -> ST s0 ()) -> v0 s0 a -> ST s0 ())
           -> VG.Mutable v s a -> ST s ()
   |
47 | quickSortBy cmp = VG.modify $ fix $ \loop vec ->
   |                               ^^^

Main.hs:56:32: error: [GHC-88464]
    Variable not in scope:
      fix
        :: ((Int -> Int -> m2 Int) -> b0 -> b1 -> m2 Int)
           -> b2 -> Int -> ST s Int
   |
56 | pivotPartition cmp vec pivot = fix `flip` 0 `flip` VGM.length vec $ \loop !l !r -> do
   |                                ^^^

Main.hs:57:15: error: [GHC-88464]
    Variable not in scope:
      fix :: ((Int -> m1 Int) -> Int -> m1 Int) -> b0 -> m2 Int
   |
57 |   !l' <- flip fix l $ \loopL !i -> do
   |               ^^^

Main.hs:62:15: error: [GHC-88464]
    Variable not in scope:
      fix :: ((Int -> m0 Int) -> Int -> m0 Int) -> b1 -> m2 Int
   |
62 |   !r' <- flip fix (r - 1) $ \loopR !i -> do
   |               ^^^

ソースコード

diff #

{-# LANGUAGE BangPatterns #-}

module Main where

import           Control.Monad
import           Control.Monad.ST
import           Control.Monad.State
import           Data.Bits
import           Data.Bool
import           Data.Char
import           Data.Coerce
import qualified Data.ByteString.Char8       as BSC8
import qualified Data.Vector.Generic         as VG
import qualified Data.Vector.Generic.Mutable as VGM
import qualified Data.Vector.Unboxed         as VU

main :: IO ()
main = do
  xs <- seqInput 4
  putStrLn $ bool "No" "Yes" (solve xs)

solve :: VU.Vector Int -> Bool
solve xs = runST $ do
  let
    ys = quickSort xs
    a = ys VU.! 0 
    b = ys VU.! 1
    c = ys VU.! 2
    d = ys VU.! 3
  return ((a + 1 == b) && (b + 1 == c) && (c + 1 == d))

type CParser a = StateT BSC8.ByteString Maybe a
runCParser :: CParser a -> BSC8.ByteString -> Maybe (a, BSC8.ByteString)
runCParser = runStateT
{-# INLINE runCParser #-}
int :: CParser Int
int = coerce $ BSC8.readInt . BSC8.dropWhile isSpace
{-# INLINE int #-}
seqInput :: Int -> IO (VU.Vector Int)
seqInput n = VU.unfoldrN n (runCParser int) <$> BSC8.getLine
{-# INLINE seqInput #-}

quickSort :: (Ord a, VG.Vector v a) => v a -> v a
quickSort = quickSortBy compare

quickSortBy :: VG.Vector v a => (a -> a -> Ordering) -> v a -> v a
quickSortBy cmp = VG.modify $ fix $ \loop vec ->
  when (VGM.length vec > 1) $ do
    pivot <- getMedian3Pivot cmp vec
    cut   <- pivotPartition cmp vec pivot
    loop (VGM.unsafeDrop cut vec)
    loop (VGM.unsafeTake cut vec)
{-# INLINE quickSortBy #-}

pivotPartition :: (VGM.MVector mv a) => (a -> a -> Ordering) -> mv s a -> a -> ST s Int
pivotPartition cmp vec pivot = fix `flip` 0 `flip` VGM.length vec $ \loop !l !r -> do
  !l' <- flip fix l $ \loopL !i -> do
    x   <- VGM.unsafeRead vec i
    case cmp x pivot of
      LT -> loopL (i + 1)
      _  -> return i
  !r' <- flip fix (r - 1) $ \loopR !i -> do
    x <- VGM.unsafeRead vec i
    case cmp pivot x of
      LT -> loopR (i - 1)
      _  -> return i
  if l' < r'
    then do
      VGM.unsafeSwap vec l' r'
      loop (l' + 1) r'
    else return l'
{-# INLINE pivotPartition #-}

getMedian3Pivot :: (VGM.MVector mv a) => (a -> a -> Ordering) -> mv s a -> ST s a
getMedian3Pivot cmp vec = median cmp <$> VGM.unsafeRead vec 0 <*> VGM.unsafeRead vec (VGM.length vec `quot` 2) <*> VGM.unsafeRead vec (VGM.length vec - 1)
{-# INLINE getMedian3Pivot #-}

median :: (a -> a -> Ordering) -> a -> a -> a -> a
median cmp x y z = case cmp x y of
  LT -> case cmp y z of
    LT -> y
    _  -> case cmp x z of
      LT -> z
      _  -> x
  _  -> case cmp x z of
    LT -> x
    _  -> case cmp y z of
      LT -> z
      _  -> y
{-# INLINE median #-}
0