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

2
Last updated at Posted at 2026-08-29

はじめに

今週はCまででした。Dは再帰を上手く使えた!と思いましたが残念ながら3問TLEとなり抜け出せず終わりました。

A問題

前半半分を捨てて後半半分の合計値を計算します。

A問題提出

main = do
  n <- readLn :: IO Int
  let half = n `div` 2
  as <- readInts
  let bs = drop half as
  print $ sum bs

B問題

sortしてグループ毎の長さが奇数の物のみを集めて合計とります。

B問題提出

main = do
  n <- readLn :: IO Int
  as <- readInts
  let gs = map (\(a,_) -> a ) $ filter (\(a,l) -> odd l ) $ map (\g -> (head g, length g)) $ group $ sort as
  print $ sum gs

C問題

出現個数が多いもの順に並べ、第2位のものまでカウントします。

C問題提出

main = do
  [n,k] <- readInts
  as <- readInts
  let gs = sortBy (flip compare) $ map (\g -> length g) $ group $ sort as
  print $ solve (head gs) gs

solve x [] = 0
solve x (g:gs)
  | x == g = 1 + solve x gs
  | x == g + 1 = 1 + solve x gs
  | otherwise = 0

おわりに

D問題できるだけ枝狩りを増やしたりしてみましたが結局3問TLEが取れず。方針自体まずかった気がしますが原因までは分からずです。

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?