AtCoder Beginner Contest 332 振り返り
振り返りです!
結果
CがWAになって今回も2完でした…Ratingもちょっと下がっちゃいました。
![](https://assets.st-note.com/img/1702309767512-aM4IAjzO2e.png?width=1200)
![](https://assets.st-note.com/img/1702309785370-ZB5EsAmoSb.png?width=1200)
各問題の振り返り
A - Online Shopping
金額に応じて分岐するだけです。特に難しいことはありませんでした。
main = do
[n, s, k] <- readInputIntList
qs <- V.fromList <$> replicateM n readPairInt
let money = V.foldl' (\acc (p, q) -> acc + (p * q)) 0 qs
print $ if money >= s then money else money + k
B - Glass and Mug
グラスとカップの水の状態を見て分岐させる問題です。
Stateモナドを使うといい感じに解けるんじゃない?と思って解いたらいい感じで解けました。まだ慣れてないのでちょっと時間がかかってしまいました。
main :: IO ()
main = do
[k, g, m] <- readInputIntList
putStrLn $ (\(g, m) -> show g ++ " " ++ show m) $ solve g m k
type Glass = Int
type Mug = Int
solve :: Int -> Int -> Int -> (Glass, Mug)
solve g m k = execState (replicateM k (op g m)) (0, 0)
op :: Int -> Int -> State (Glass, Mug) ()
op g m = do
(glass, mug) <- get
if glass == g
then put (0, mug)
else
if mug == 0
then put (glass, m)
else
let transfer = min (g - glass) mug
in put (glass + transfer, mug - transfer)
C - T-shirts
ちょっと複雑ですが、問題文を見て丁寧に条件文を作ればそれほど難しくない問題でした。
WAになったのはこちら
main :: IO ()
main = do
[_, m] <- readInputIntList
s <- BS.getLine
print $ solve m s
solve :: Int -> BS.ByteString -> Int
solve m s = go m s 0 0
where
go :: Int -> BS.ByteString -> Int -> Int -> Int
go _ bs _ logoShirts | BS.null bs = logoShirts
go plainShirts bs wornLogoShirts logoShirts =
case BS.head bs of
'0' -> go (plainShirts + wornLogoShirts) (BS.tail bs) 0 logoShirts
'1' ->
if plainShirts > 0
then go (plainShirts - 1) (BS.tail bs) wornLogoShirts logoShirts
else go 0 (BS.tail bs) (wornLogoShirts + 1) (logoShirts + 1)
'2' ->
if logoShirts > wornLogoShirts
then go plainShirts (BS.tail bs) (wornLogoShirts + 1) logoShirts
else go plainShirts (BS.tail bs) (wornLogoShirts + 1) (logoShirts + 1)
upsolvedしたのはこちらです。ロゴ入りTシャツの処理を誤っていたみたいでした。
main :: IO ()
main = do
[_, m] <- readInputIntList
s <- BS.getLine
print $ solve s m
solve :: BS.ByteString -> Int -> Int
solve s m = ans
where
(_, _, ans) = BS.foldl' (flip go) (m, 0, 0) s
go '0' (_, _, x) = (m, x, x)
go '1' (0, 0, x) = (0, 0, x + 1)
go '1' (0, logo, x) = (0, logo - 1, x)
go '1' (plain, logo, x) = (plain - 1, logo, x)
go '2' (plain, 0, x) = (plain, 0, x + 1)
go '2' (plain, logo, x) = (plain, logo - 1, x)
f _ _ = error "error"
全体を振り返って
また次がんばります!
この記事が気に入ったらサポートをしてみませんか?