1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AtCoder Beginner Contest 351 振り返り

Posted at

ABC351の振り返りです。

今回の結果

A,B,C 3完でした。

スクリーンショット 2024-05-01 12.29.04.png

スクリーンショット 2024-05-01 12.28.27.png

各問題の振り返り

A - The bottom of the ninth

差を取って1足せば良いです。

main :: IO ()
main = do
  aScore <- L.sum <$> readInputInts
  bScore <- L.sum <$> readInputInts

  print $ aScore - bScore + 1

B - Spot the Difference

1≤N≤100であることから、全探索で解けるとわかります。回答にはindexが必要なので、Haskellで取得できるようにzipを使用しています。

main :: IO ()
main = do
  n <- readLn @Int
  as <- replicateM n getLine
  bs <- replicateM n getLine

  let [(i', j')] = [(i + 1, j + 1) | (i, rowAs) <- zip [0 ..] as, (j, ca) <- zip [0 ..] rowAs, let cy = bs L.!! i L.!! j, ca /= cy]

  putStrLn $ show i' ++ " " ++ show j'

C - Merge the balls

問題文通りに実装するだけです。あえて工夫した点をあげるなら、以下の2つになります。

  • 2^Aiで計算する必要がないため、Aiのまま計算する
    • 同値判定は指数があればできる
    • 「取り除かれた 2 つのボールの大きさの和」Ai + 1として扱うことができる(2^Ai*2 = 2^(Ai+1)となることを考えればわかる)
  • 問題文に「右からボールを加える」とあるが、左から加えていった方が(head:tail)と書けて扱いやすいので、問題文に従わず左から加えていく

実装は以下の様な形になります。

main :: IO ()
main = do
  _ <- readLn @Int
  as <- readInputInts

  print $ L.length $ solve [] as

solve :: [Int] -> [Int] -> [Int]
solve res [] = res
solve [] (hAs : tAs) = solve [hAs] tAs
solve res@(hRes : tRes) (hAs : tAs) =
  if hRes /= hAs
    then solve (hAs : res) tAs
    else solve tRes ((hRes + 1) : tAs)

全体を振り返って

今回のA-Cは比較的簡単な印象を受けました。次もこれくらい解けるといいなあ。

おまけ

今回もRustで解き直したのを置いておきます。異なる言語で書くと、それぞれの言語の特徴がつかめて良いです。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?