結果

問題 No.50 おもちゃ箱
ユーザー satonakatakumisatonakatakumi
提出日時 2021-08-22 14:47:44
言語 Haskell
(9.8.2)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,144 bytes
コンパイル時間 4,447 ms
コンパイル使用メモリ 158,464 KB
最終ジャッジ日時 2024-11-15 01:43:27
合計ジャッジ時間 5,057 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、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:31:1: error: [GHC-87110]
    Could not load module ‘GHC.Integer.GMP.Internals’.
    It is a member of the hidden package ‘integer-gmp-1.1’.
    Use -v to see a list of the files searched for.
   |
31 | import qualified GHC.Integer.GMP.Internals         as GMP
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

ソースコード

diff #

{-# LANGUAGE BangPatterns     #-}
{-# LANGUAGE TupleSections    #-}
{-# LANGUAGE TypeApplications #-}

import           Control.Monad
import           Control.Monad.Fix
import           Control.Monad.ST
import           Control.Monad.State
import           Data.Bits
import           Data.Bool
import qualified Data.ByteString                   as BS
import qualified Data.ByteString.Builder           as BSB
import qualified Data.ByteString.Char8             as BSC8
import qualified Data.ByteString.Lazy.Char8        as BSLC8
import qualified Data.ByteString.Unsafe            as BSU
import           Data.Char
import           Data.Coerce
import qualified Data.Foldable                     as F
import           Data.IORef
import           Data.Int
import qualified Data.List                         as L
import           Data.STRef
import qualified Data.Vector                       as V
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.Mutable               as VM
import qualified Data.Vector.Unboxed               as VU
import qualified Data.Vector.Unboxed.Mutable       as VUM
import           Data.Word
import qualified GHC.Integer.GMP.Internals         as GMP
import           System.CPUTime
import           System.IO
import           Unsafe.Coerce

inf :: Int
inf = 123456
{-# INLINE inf #-}

main :: IO ()
main = do
  n <- readLn :: IO Int
  as <- seqInput n
  m <- readLn :: IO Int
  bs' <- seqInput m
  let bs = VU.reverse $ bucketSort bs'
  dp  <- VUM.replicate (1 `unsafeShiftL` (11 :: Int)) inf
  VUM.unsafeWrite dp 0 (0 :: Int)
  rep (1 `unsafeShiftL` n) $ \i -> do
    dpi <- VUM.unsafeRead dp i
    when (dpi < m) $ do
      rep (1 `unsafeShiftL` n) $ \j -> do
        when (i .&. j == 0) $ do
          s <- newIORef (0 :: Int)
          rep n $ \k -> when (j `unsafeShiftR` k .&. 1 /= 0) $ modifyIORef' s (+ as VU.! k)
          s' <- readIORef s
          when (s' <= bs VU.! dpi) $ VUM.unsafeModify dp (min (dpi + 1)) (i .|. j)
  ans <- VUM.unsafeRead dp (1 `unsafeShiftL` n - 1)
  if ans == inf
    then putStrLn "-1"
    else print ans

type Parser a = StateT BSC8.ByteString Maybe a
runParser :: Parser a -> BSC8.ByteString -> Maybe (a, BSC8.ByteString)
runParser = runStateT
{-# INLINE runParser #-}
int :: Parser Int
int = coerce $ BSC8.readInt . BSC8.dropWhile isSpace
{-# INLINE int #-}
seqInput :: Int -> IO (VU.Vector Int)
seqInput n = VU.unfoldrN n (runParser int) <$> BSC8.getLine
{-# INLINE seqInput #-}
rep :: Monad m => Int -> (Int -> m ()) -> m ()
rep n = flip VFSM.mapM_ (stream 0 n)
{-# INLINE rep #-}
stream :: Monad m => Int -> Int -> VFSM.Stream m Int
stream !l !r = VFSM.Stream step l
  where
    step x
      | x < r     = return $ VFSM.Yield x (x + 1)
      | otherwise = return VFSM.Done
    {-# INLINE [0] step #-}
{-# INLINE [1] stream #-}
bucketSort :: VU.Vector Int -> VU.Vector Int
bucketSort = VU.concatMap (uncurry $ flip VU.replicate) . VU.indexed . VU.unsafeAccumulate (+) (VU.replicate 21 0) . VU.map (,1)
{-# INLINE bucketSort #-}
0