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?

HaskellでABC467を解く

1
Posted at

はじめに

今週はCでハマりました。

A問題

正確に判定するため整数にしてから比較します。

A問題提出

main = do
  [h,w] <- readInts
  putStrLn $ bool "No" "Yes" $ 100 * 100 * w >= 25 * h * h

B問題

条件に当てはまるときのみ集計します。

B問題提出

main = do
  n <- readLn :: IO Int
  abss <- replicateM n $ do
    [a,b,s] <- words <$> getLine
    return (read @Int a,read @Int b,s)
  print $ sum $ solve abss

solve = foldl step []
  where
    step acc (a,b,s)
      = case s of
        "keep" -> (b - a):acc
        _ -> acc

C問題

2つづつ変更したらいいかなとか色々やって時間内にはACできず。
解説動画を聞いて解き直しました。aのリストの最初を0か1で決めたらaの配列が自動的に決まってしまうのでもとのaのリストをそのどちらかにする手数が小さい方が解になります。

C問題提出

main = do
  [_n,_m] <- readInts
  as <- readInts
  bs <- readInts
  let cand1 = 0 : build 0 bs
  let cand2 = 1 : build 1 bs
  let ans1 = diff as cand1
  let ans2 = diff as cand2
  print $ min ans1 ans2

build _a1 [] = []
build a1 (b:bs) = a2 : build a2 bs
  where
    a2 = (a1+b)`mod`2

diff (a:as) (c:cs)
  | a == c = diff as cs
  | otherwise = 1 + diff as cs
diff _ _ = 0

おわりに

C問題最後まで解き方にたどり着けずでした。解が決まってしまうという発想ができなかったです。

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?