2
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でABC433を解く

Posted at

はじめに

ABC433に参加しました。

A問題

(Y+a)*Z = X+a という式を展開し a = に直したときに分母が正かつ整数になっていればOK。分母はZ>=2の条件から必ず正なので気にしなくて良い。

main = do
  [x,y,z] <- readInts
  let a = (x - y*z)`mod`(z-1)
  if a == 0 && x - y*z >= 0 then
    putStrLn "Yes"
  else
    putStrLn "No"

A問題提出

B問題

Nが小さいので全探索で求める。自分の左側で自分より大きいものがあり一番最後に出てきたものが解になる。

main = do
  n <- readLn :: IO Int
  as <- readInts
  let bs = zip as [1..]
  let cs = solve [filter (\(x,j) -> j < i && x > a) bs | (a,i) <- bs]
  putStr $ unlines $ map show cs

solve [] = []
solve (a:as)
  | null a = -1 : solve as
  | otherwise = snd (last a) : solve as

B問題提出

C問題

ランレングス圧縮しておいて隣との関係で数を数えていけばよい。

main = do
  s <- getLine
  let ans = solve $ map (\g -> (head g, length g )) $ group s
  print ans

solve ((c1,l1):(c2,l2):cs)
  | x + 1 == y = min l1 l2 + solve ((c2,l2):cs)
  | otherwise = solve ((c2,l2):cs)
  where
    x = read @Int [c1]
    y = read @Int [c2]
solve _ = 0

C問題提出

おわりに

D問題は過去に似たような問題があってグループでまとめて処理すれば良さそうというところまで気づいたのですが実装うまく行かずでした。

2
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
2
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?