Skip to content

Instantly share code, notes, and snippets.

@LeastFixedPoint
Created July 10, 2013 13:16
Show Gist options
  • Save LeastFixedPoint/5966192 to your computer and use it in GitHub Desktop.
Save LeastFixedPoint/5966192 to your computer and use it in GitHub Desktop.
import Control.Monad (foldM)
split s = let prefix = takeWhile (/= ' ') s in (prefix, drop (1 + length prefix) s)
main = do
nm <- getLine
let (sn, sm) = split nm
let (n, m) = (read sn, read sm)
let step songs index = do
line <- getLine
let (frequency, name) = split line
let quality = read frequency * index
let song = (quality, name)
let (better, worse) = span ((>= quality) . fst) songs
return $ take m $ better ++ [song] ++ worse
songs <- foldM step [] [1..n]
sequence_ $ fmap (putStrLn . snd) songs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment