以下是 Haskell 代码,我100%确定能通过数据点,为啥线上提交输出是0呢?
{-# LANGUAGE LambdaCase #-}
module Main where
import Control.Arrow
import Control.Monad.ST
import Data.Foldable (for_)
import qualified Data.HashMap.Strict as HM
import Data.IORef
readTape :: IO [(Int, Int)]
readTape = do
numbers <- fmap read . words <$> getLine
colors <- fmap read . words <$> getLine
return $ zip colors numbers
-- | return odd colors, even colors (pos, number)
readTape' :: IO (HM.HashMap Int [(Int, Int)], HM.HashMap Int [(Int, Int)])
readTape' = do
oddm <- newIORef HM.empty
evenm <- newIORef HM.empty
tapes <- readTape
for_ (zip [1..] tapes) $ \(index, (color, number)) -> modifyIORef (if odd index then oddm else evenm) $ \m ->
HM.alter (\case
Just xs -> return $ (index, number) : xs
Nothing -> return [(index, number)]
) color m
o <- readIORef oddm
e <- readIORef evenm
return (o, e)
calculateScore :: (HM.HashMap Int [(Int, Int)], HM.HashMap Int [(Int, Int)]) -> Int
calculateScore (oddm, evenm) = (sum (map (sumUp . snd) $ HM.toList oddm) + sum (map (sumUp . snd) $ HM.toList evenm)) `mod` 10007
where sumUp :: [(Int, Int)] -> Int
sumUp m = foldl (\x (a, b) -> (x + b * (a * (n - 2) + posSum)) `mod` 10007) 0 m
where posSum = sum $ fst <$> m
n = length m
main :: IO ()
main = print . calculateScore =<< readTape'