{-# LANGUAGE OverloadedStrings #-} import Data.List (sort) import Data.Maybe (fromJust) import qualified Data.Set as S import qualified Data.ByteString.Char8 as B main = B.interact $ solve . (\(x:xs) -> (x, zip [0..] $ map parse xs)) . B.lines readInt = fst . fromJust . B.readInt parse = (\(a:b:c:_) -> ([readInt a, readInt b], B.head c) :: ([Int], Char)) . B.words solve (ns, xs) = B.pack . unwords . map show $ (\(a,b,c) -> [a, b, c]) ans where n = readInt . head $ B.words ns ys = sort $ map (\e -> (head . fst $ snd e, e)) xs r = (n+1,(length xs, ([n,n], 'X'))) (ans,_,_) = foldl f ((0,0,0), 1, S.empty) (ys ++ [r]) f a@(c, i, s) y@(j, e) | i == j = (c, i, (S.insert e s)) | otherwise = f (g a) y g (c@(yc, kc, cc), i, s) = (c2, i+1, s2) where h s = case S.minView s of Just ((_, ([_, b], _)), t) | b >= i -> s | otherwise -> h t _ -> s s2 = h s c2 = case S.lookupMin s2 of Just (_, (_, 'Y')) -> (yc+1,kc,cc) Just (_, (_, 'K')) -> (yc,kc+1,cc) Just (_, (_, 'C')) -> (yc,kc,cc+1) _ -> c