結果

問題 No.1279 Array Battle
ユーザー こまるこまる
提出日時 2020-11-18 17:50:25
言語 Haskell
(9.8.2)
結果
AC  
実行時間 963 ms / 2,000 ms
コード長 3,644 bytes
コンパイル時間 1,971 ms
コンパイル使用メモリ 235,964 KB
実行使用メモリ 12,052 KB
最終ジャッジ日時 2023-09-30 15:08:22
合計ジャッジ時間 7,924 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,792 KB
testcase_01 AC 3 ms
7,832 KB
testcase_02 AC 3 ms
8,292 KB
testcase_03 AC 3 ms
7,736 KB
testcase_04 AC 3 ms
7,736 KB
testcase_05 AC 3 ms
7,620 KB
testcase_06 AC 2 ms
7,852 KB
testcase_07 AC 2 ms
7,864 KB
testcase_08 AC 3 ms
7,868 KB
testcase_09 AC 3 ms
7,952 KB
testcase_10 AC 3 ms
8,408 KB
testcase_11 AC 7 ms
11,180 KB
testcase_12 AC 22 ms
11,984 KB
testcase_13 AC 102 ms
11,908 KB
testcase_14 AC 102 ms
12,052 KB
testcase_15 AC 963 ms
11,996 KB
testcase_16 AC 942 ms
12,016 KB
testcase_17 AC 922 ms
11,964 KB
testcase_18 AC 922 ms
11,908 KB
testcase_19 AC 942 ms
11,920 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.6.1/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
[2 of 2] Linking a.out

ソースコード

diff #

{-# LANGUAGE BangPatterns      #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TupleSections     #-}

import           Control.Monad
import           Control.Monad.Fix
import           Control.Monad.ST
import           Control.Monad.State
import           Data.Char
import           Data.Coerce
import           Data.IORef
import           Data.Ord
import qualified Data.ByteString.Char8             as BSC8
import qualified Data.Vector.Fusion.Stream.Monadic as VFSM
import qualified Data.Vector.Generic               as VG
import qualified Data.Vector.Generic.Mutable       as VGM
import qualified Data.Vector.Unboxed               as VU
import qualified Data.Vector.Unboxed.Mutable       as VUM

main :: IO ()
main = do
  n <- readLn :: IO Int
  a <- quickSort <$> seqInput n
  b <- quickSortBy (comparing Down) <$> seqInput n
  p <- VU.unsafeThaw $ VU.fromList ([0..(n - 1)] :: [Int])
  ans <- newIORef (0 :: Int)
  let res = VU.sum $ VU.zipWith (\x y -> max 0 (x - y)) a b
  fix $ \loop -> do
    s <- newIORef (0 :: Int)
    rep n $ \i -> do
      pi <- VUM.unsafeRead p i
      modifyIORef' s (+ max 0 (a VU.! pi - b VU.! i))
    su <- readIORef s
    when (su == res) $ modifyIORef' ans succ
    check <- VUM.nextPermutation p
    when check loop
  print =<< readIORef ans

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 #-}

-- | l -> x -> r, +d
stream :: Monad m => Int -> Int -> Int -> VFSM.Stream m Int
stream !l !r !d = VFSM.Stream step l
  where
    step x
      | x <= r    = return $ VFSM.Yield x (x + d)
      | otherwise = return VFSM.Done
    {-# INLINE [0] step #-}
{-# INLINE [1] stream #-}

-- | 0 <= x < n, interval = 1
rep :: Monad m => Int -> (Int -> m ()) -> m ()
rep n = flip VFSM.mapM_ (stream 0 (n - 1) 1)
{-# INLINE rep #-}

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 #-}
0