Skip to content

Instantly share code, notes, and snippets.

@gorlum0
Created August 28, 2011 07:23
Show Gist options
  • Save gorlum0/1176355 to your computer and use it in GitHub Desktop.
Save gorlum0/1176355 to your computer and use it in GitHub Desktop.
codeforces - 2 - A (hs)
{-# OPTIONS_GHC -O2 -XNoMonomorphismRestriction #-}
{-# LANGUAGE BangPatterns #-}
{-(c) gorlum0 [at] gmail.com-}
import qualified Data.Map as M
import qualified Data.Set as S
splitEvery _ [] = []
splitEvery n xs = ys : splitEvery n xs'
where (ys, xs') = splitAt n xs
winner :: [(String, Int)] -> String
winner rounds = f M.empty rounds'
where
rounds' = [(k, v) | (k, v) <- rounds, S.member k cands]
game = M.fromListWith (+) rounds
m = maximum $ M.elems game
--- cands = S.fromList [k | (k, v) <- M.toList game, v == m]
cands = M.keysSet $ M.filter (==m) game
f :: M.Map String Int -> [(String, Int)] -> String
f game ((k,v):xs)
| n >= m = k
| otherwise = f game' xs
where game' = M.insertWith (+) k v game
Just n = M.lookup k game'
body :: [String] -> [String]
body [] = []
body (n:xs) = winner rounds : body xs'
where
n' = read n
(ys, xs') = splitAt (n'*2) xs
rounds = [(k, read v) | [k, v] <- splitEvery 2 ys]
main = do
ws <- words `fmap` getContents
mapM_ putStrLn $ body ws
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment