結果

問題 No.1267 Stop and Coin Game
ユーザー こまるこまる
提出日時 2020-11-03 10:21:21
言語 Haskell
(9.8.2)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,467 bytes
コンパイル時間 412 ms
コンパイル使用メモリ 183,292 KB
最終ジャッジ日時 2024-11-14 23:53:54
合計ジャッジ時間 869 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、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:27:26: error: [GHC-87543]
    Ambiguous occurrence ‘.<<.’.
    It could refer to
       either ‘Data.Bits..<<.’,
              imported from ‘Data.Bits’ at Main.hs:6:1-26,
           or ‘Main..<<.’, defined at Main.hs:69:1.
   |
27 |   dp <- VUM.replicate (1 .<<. 20) (0 :: Int)
   |                          ^^^^

Main.hs:41:31: error: [GHC-87543]
    Ambiguous occurrence ‘.>>.’.
    It could refer to
       either ‘Data.Bits..>>.’,
              imported from ‘Data.Bits’ at Main.hs:6:1-26,
           or ‘Main..>>.’, defined at Main.hs:65:1.
   |
41 |             let check = start .>>. i .&. 1
   |                               ^^^^

Main.hs:43:48: error: [GHC-87543]
    Ambiguous occurrence ‘.<<.’.
    It could refer to
       either ‘Data.Bits..<<.’,
              imported from ‘Data.Bits’ at Main.hs:6:1-26,
           or ‘Main..<<.’, defined at Main.hs:69:1.
   |
43 |               recus <- dfs dp a n (start .|. 1 .<<. i) (now - a VU.! i)
   |                                                ^^^^

ソースコード

diff #

{-# LANGUAGE BangPatterns         #-}

import           Control.Monad
import           Control.Monad.ST
import           Control.Monad.State
import           Data.Bits
import           Data.Bool
import           Data.Char
import           Data.Coerce
import           Data.STRef.Strict
import qualified Data.ByteString.Char8             as BSC8
import qualified Data.Vector.Fusion.Stream.Monadic as VFSM
import qualified Data.Vector.Unboxed               as VU
import qualified Data.Vector.Unboxed.Mutable       as VUM

main :: IO ()
main = do
  [n, v] <- map read . words <$> getLine
  xs <- seqInput n
  let s = VU.sum xs
  if s <= v
    then putStrLn "Draw"
    else putStrLn $ solve n v xs

solve :: Int -> Int -> VU.Vector Int -> String
solve n v xs = bool "Second" "First" $ runST $ do
  dp <- VUM.replicate (1 .<<. 20) (0 :: Int)
  dfs dp xs n 0 v

dfs :: VUM.STVector s Int -> VU.Vector Int -> Int -> Int -> Int -> ST s Bool
dfs dp a n start now = do
  dpst <- VUM.unsafeRead dp start
  if dpst /= 0
    then return $ dpst > 0
    else do
      if now < 0
        then VUM.unsafeWrite dp start 1 >> return True
        else do
          ret <- newSTRef False
          rep n $ \i -> do
            let check = start .>>. i .&. 1
            when  (check == 0) $ do
              recus <- dfs dp a n (start .|. 1 .<<. i) (now - a VU.! i)
              when (not recus) $ writeSTRef ret True
          res <- readSTRef ret
          if res
            then VUM.unsafeWrite dp start 1    >> return res
            else VUM.unsafeWrite dp start (-1) >> return res

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

rep :: Monad m => Int -> (Int -> m ()) -> m ()
rep n = flip VFSM.mapM_ (stream 0 n)

infixl 8 .>>., .<<.

(.>>.) :: Bits b => b -> Int -> b
(.>>.) = unsafeShiftR
{-# INLINE (.>>.) #-}

(.<<.) :: Bits b => b -> Int -> b
(.<<.) = unsafeShiftL
{-# INLINE (.<<.) #-}

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