ByteString
一つ前の記事で書いた方法ではタイムオーバーになってしまう。
func_hsさんの記事を読んで、
ByteStringを使う方法があることを思い出す
しかし、初めて使うので思考錯誤の末、
あまり意味がないように感じるけど、次のようなものを書いてみた
import Data.List import qualified Data.ByteString.Char8 as B import Data.Maybe type Str = B.ByteString type Touples = (Str,Str) main = do n <- B.getLine ab <- B.getContents let ab_s = sort . makePairs $ dataList n ab print $ ab_s print $ answer (snd $ head ab_s) $tail ab_s makePairs :: [Str] -> [Touples] makePairs [] = [] makePairs (a:b:xs) = (a,b):makePairs xs dataList :: Str -> Str -> [Str] dataList n = take (2*myRead n) . B.words myRead = fst . fromJust . B.readInt answer :: Str ->[Touples] -> Int answer _ [] = 0 answer maxb ((_,b):ab) = if myRead maxb > myRead b then 1+answer maxb ab else answer b ab
なんか結局再帰使っちゃってるし、
意味あるのかないのかわからない。
foldlで書き直せそうな内容。
短い入力例でしか試していないけれど、答えはちゃんとでる。
ただし、上の内容では、文字列でソートしてしまっているので 正しい答えが出るとは限りません。
ただ、この内容では読み込みが停止しないのです。
B.getContentsを、
dataList関数内のPrelude.takeで区切りを決めているはずなのに、
どこまでも入力を受付けようとする。
Prelude.takeとの相性が悪いのだろうか?