Skip to content

Instantly share code, notes, and snippets.

@m-renaud
Last active August 29, 2015 14:09
Show Gist options
  • Save m-renaud/d686d597eca822b857ee to your computer and use it in GitHub Desktop.
Save m-renaud/d686d597eca822b857ee to your computer and use it in GitHub Desktop.
Reading lots of input.
-- | Timing breakdown:
-- Reading: 2.5s
-- Sorting: 3.5s
module Main where
import Control.Applicative
import Control.Monad
import Data.ByteString.Char8 (pack)
import Data.Attoparsec.ByteString.Char8
import Data.Functor
import Data.List
doLinearTimeStuffWith :: Int -> [Int] -> IO ()
doLinearTimeStuffWith n ns = print (ns !! (n - 1))
parseIntList :: Int -> Parser [Int]
parseIntList n = count n (signed decimal <* endOfLine)
main = do
n <- readLn :: IO Int
ns <- parseOnly (parseIntList n) . pack <$> getContents
case ns of
Left err -> print err
Right ns' -> doLinearTimeStuffWith n $ sort ns'
module Main where
import Control.Monad
import Data.List
doLinearTimeStuffWith :: Int -> [Int] -> IO ()
doLinearTimeStuffWith n ns = print (ns !! (n - 1))
main = do
n <- readLn :: IO Int
ns <- replicateM n (readLn :: IO Int)
doLinearTimeStuffWith n $ sort ns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment