Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

ABC012をHaskellで

0
Posted at

A - スワップ

問題 ABC012A

main から書く。

main :: IO ()
main = putStrLn . unwords . reverse . words =<< getLine

B - 入浴時間

問題 ABC012B

シグネチャを決める。

abc012b :: Int    -- N
        -> String -- 答え

60で割り算すればいいだけだが、出力について、leading zero を挿入する必要がある。
printf なライブラリを使うべきかもしれないが、ここだけのための下請け関数でお茶を濁す。

結果

abc012b n = show0 h ++ ':' : show0 m ++ ':' : show0 s
  where
    (hm,s) = divMod n  60
    (h,m)  = divMod hm 60
    show0 x
      | x < 10    = '0' : show x
      | otherwise =       show x

C - 九九足し算

問題 ABC012C

シグネチャを決める。

abc012c :: Int      -- N
        -> [String] -- 答え

九九の総和は 2025 ($\sum_{x = 1}^9 \sum_{y=1}^9 xy =$ $\sum_{x = 1}^9 x \sum_{y=1}^9 y =$ $\sum_{x = 1}^9 x \cdot 45 = 45 \sum_{x = 1}^9 x =$ $45 \cdot 45 =$ $(50-5)^2 = 50^2 - 2 \cdot 5 \cdot 50 + 5^2 = 2500 - 500 + 25 = 2025$) というのが2025年に流行った。

結果

abc012c :: Int -> [String]
abc012c n = [show a ++ " x " ++ show b | a <- [1..9], (b, 0) <- [divMod x a]]
  where
    x = total - n
    total = sum [a * b | a <- [1 .. 9], b <- [1 .. 9]] -- = 2025

D - バスと避けられない運命

問題 ABC012C

シグネチャを決める。

abc012d :: Int     -- N
        -> Int     -- M
        -> [[Int]] -- a_i, b_i, t_i
        -> Int     -- 答え

読み取りにくいが、ようは、グラフの全ての頂点間の距離を使って、始点に対して最も遠い点までの距離、が最小になる始点の選択をしたときのその最長距離を求める話。

$N \leq 300$ というのは、ワーシャルフロイド法を使えと書いてあるのと同じ。

結果

ワーシャルフロイド法において、距離の配列は全てを更新する可能性も大いにあるので immutable array で実装した。
というかむしろ i, j のループの途中で配列を変更してしまう命令的な版は、本来の定義とは経過が異なる(が結果は同じ)ので、これの方が由緒正しい。

import Data.Array.Unboxed
import Data.List.Split

abc012d :: Int -> Int -> [[Int]] -> Int
abc012d n _m abts = minimum $ map maximum $ chunksOf n $ elems d
  where
    d = warshallFloyd (1,n) $
        [((i,i), 0) | i <- [1 .. n]] ++
        [((a,b), t) | a:b:t:_ <- abts] ++
        [((b,a), t) | a:b:t:_ <- abts]

warshallFloyd :: (Int,Int)               -- 頂点レンジ
              -> [((Int,Int),Int)]       -- 有向辺 ((from,to),length)
              -> UArray (Int,Int) Int    -- 距離の表 経路がないとき maxBound
warshallFloyd (lb,ub) edges = distN
  where
    dist0 = accumArray (flip const) maxBound ((lb,lb),(ub,ub)) edges
    distN = foldl' step dist0 [lb .. ub]
    step dist k = dist //
      [ ((i,j), dikj)
      | i <- [lb .. ub], let dik = dist ! (i,k), dik < maxBound
      , j <- [lb .. ub], let dkj = dist ! (k,j), dkj < maxBound
      , let dikj = dik + dkj, dist ! (i,j) > dikj ]
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?