Submission #297243


Source Code Expand

import Control.Applicative
import Control.Monad
import Data.List
import qualified Data.Map.Strict as M
import Text.Printf


type HHMM = Int
type Minute = Int
type Count = Int


type Period = (Minute, Minute)


readPeriod :: String -> Period
readPeriod s = ((toMinute . read . take 4) s, (toMinute . read . drop 5) s)


showPeriod :: Period -> String
showPeriod (st, en) = printf "%04d-%04d" (toHHMM st) (toHHMM en)


toMinute :: HHMM -> Minute
toMinute x = (h * 60) + m
  where
    h = x `div` 100
    m = x - (h * 100)

toHHMM :: Minute -> HHMM
toHHMM x = (h * 100) + m
  where
    h = x `div` 60
    m = x - (h * 60)


main :: IO ()
main = do
    num <- getNumber
    ps <- getPeriods num
    let ps' = merge $ roundPeriod <$> ps
    mapM_ putStrLn (Prelude.map showPeriod ps')


getNumber :: IO Int
getNumber = read <$> getLine


getPeriods :: Int -> IO [Period]
getPeriods num = do
    replicateM num (readPeriod <$> getLine)


roundPeriod :: Period -> Period
roundPeriod (st, en) = ((st `div` 5) * 5, ((en + 4) `div` 5) * 5)


merge :: [Period] -> [Period]
merge ps = ps'
  where
    cm = foldl' addPeriodToCountMap M.empty ps
    (ps', _, _) = M.foldrWithKey cumsum ([], 0, 0) cm


addPeriodToCountMap :: M.Map Minute Count -> Period -> M.Map Minute Count
addPeriodToCountMap m (st, en) = addToCountMap en (-1) (addToCountMap st 1 m)


addToCountMap :: Minute -> Count -> M.Map Minute Count -> M.Map Minute Count
addToCountMap mi c m = case M.lookup mi m of
    Nothing -> M.insert mi c m
    Just c' -> M.insert mi (c' + c) m


cumsum :: Minute -> Count -> ([Period], Count, Minute) -> ([Period], Count, Minute)
cumsum mi c (ps, ac, en) = case ac + c of
    0         -> ((mi, en) : ps, 0, 0)
    otherwise -> case en of
        0         -> (ps, ac + c, mi)
        otherwise -> (ps, ac + c, en)

Submission Info

Submission Time
Task D - 感雨時刻の整理
User fujiyan
Language Haskell (GHC 7.4.1)
Score 0
Code Size 1884 Byte
Status CE

Compile Error

Main.hs:4:18:
    Could not find module `Data.Map.Strict'
    Use -v to see a list of the files searched for.