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

2
Last updated at Posted at 2026-11-23

はじめに

今週はC問題まで。D問題は方針を決めてとりかかれましたが間に合わずでした。

A問題

aとbを掛け、正負を判定します。

A問題提出

main = do
  [a,b] <- readInts :: IO [Int]
  putStrLn $ if a*b > 0 then "Yes" else "No"

B問題

行と列ですべての文字が'x'である行か列を回答します。
まずは行で判定し、その後転置して列で判定します。

B問題提出

main = do
  [n,m] <- readInts
  ss <- replicateM n getLine
  let cand1 = solve ss
  let cand2 = solve $ transpose ss
  when ( not (null cand1) ) $ do
    putStrLn $ "Faucet " ++ show (head cand1)
  when ( not (null cand2) ) $ do
    putStrLn $ "Drain " ++ show (head cand2)

solve = catMaybes . zipWith step [1..]
  where
    step :: Int -> String -> Maybe Int
    step i s = if all (=='x') s then Just i else Nothing

C問題

同じ数字が続く間カウントを増やしていき違う数字が来たときのカウントを使用します。違う数字が来たときにカウントを増やして使用します。この方法を左右から行い小さい数値が解になります。

C問題提出

main = do
  _n <- readLn :: IO Int
  cs <- readInts
  let cands = solve cs
  let cands2 = reverse $ solve $ reverse cs
  putStrLn $ unwords $ map show $ zipWith min cands cands2

solve cs = step 0 cs cs
  where
    step _cnt [] [] = []
    step _cnt [] bs = []
    step cnt (_a:as) [] = cnt : step cnt as []
    step cnt (a:as) (b:bs)
      | a == b = step (cnt+1) (a:as) bs
      | otherwise = cnt : step (cnt-1) as (b:bs)

D問題

左端と右端で最小の区間を作り削除します。
真ん中の区間で左端を固定し、最小の"ABC"ができる2分法で調べ、それ以降は必ず条件を満たすためその位置から右端までが場合数にあたります。左を一つづつ進めながら繰り返すのでO(NlogN)の処理時間で解くことができます。

D問題提出

main = do
  _n <- readLn :: IO Int
  s <- getLine
  let xs = map (\x -> if x == 'A' then 1 else 0) s
  let ys = map (\x -> if x == 'B' then 1 else 0) s
  let zs = map (\x -> if x == 'C' then 1 else 0) s
  let xl = scanl (+) 0 xs
  let yl = scanl (+) 0 ys
  let zl = scanl (+) 0 zs
  let xyzs = zip3 xl yl zl
  let cands = pre xyzs :: [(Int,Int,Int)]
  let cands2 = (0,0,0) : ( reverse $ post $ reverse cands )
  let xv = VU.fromList $ map (\(a,_,_) -> a) cands2 :: VU.Vector Int
  let yv = VU.fromList $ map (\(_,b,_) -> b) cands2 :: VU.Vector Int
  let zv = VU.fromList $ map (\(_,_,c) -> c) cands2 :: VU.Vector Int
  print $ sum $ solveLowerBound xv yv zv

pre [] = []
pre ((x,y,z):xyzs)
  | x > 0 && y > 0 && z > 0 = trim xyzs
  | otherwise = pre xyzs
  where
    trim xyzs = map (\(x1,y1,z1) -> (x1-x, y1-y,z1-z)) xyzs

post cands
  | null cands = cands
  | otherwise = step (head cands) cands
  where
    step (x0,y0,z0) [] = []
    step (x0,y0,z0) ((x,y,z):xyzs)
      | x < x0 && y < y0 && z < z0 = ((x,y,z):xyzs)
      | otherwise = step (x0,y0,z0) xyzs

solveLowerBound vx vy vz = runST $ do
  let l = VU.length vx
  mvx <- VU.thaw vx
  mvy <- VU.thaw vy
  mvz <- VU.thaw vz
  ref <- newSTRef (0,0,0)
  forM [0..l-1] $ \i -> do
    let x1 = vx VU.! i
    let y1 = vy VU.! i
    let z1 = vz VU.! i
    writeSTRef ref (x1+1,y1+1,z1+1)
    (x0,y0,z0) <- readSTRef ref
    retX <- VAS.binarySearchL mvx x0
    retY <- VAS.binarySearchL mvy y0
    retZ <- VAS.binarySearchL mvz z0
    return (l - maximum [retX,retY,retZ])

おわりに

D問題は2分法を(a,b,c)でまとめて処理しようとしてミスしてしまいました。正しくはa,b,cそれぞれで'A','B','C'を含む個数をカウントし、最大の位置が解になるのが正しいです。

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?