Hey,
I've been learning Haskell by trying to solve some of the problems here, but it seems that most of the time I end up getting TLEs.
For instance, I'm reasonably confident that my solution to INTEGMAX is good, except I'm not using any complicated IO code that would optimise it.
Similarly with ARMY, I don't think I can improve on it. I initially was having TLEs, but changing the IO code from normal IO to ByteStrings made it run in time. I wonder how some of the faster Haskell answers to ARMY might deal with the IO?
On WATER, I keep getting NZEC, no idea why. It runs fine on some of the large inputs I've tried. But I don't think the algorithm is too good, so I'm sort of expecting a TLE anyway. Edit: OK, I didn't realise there was an empty line between test cases. Now I get TLE. I guess that's sort of expected.
Also on BUGLIFE, I'm also using a somewhat slower algorithm, but I don't think it's much slower that it should be.
Can anyone give me some tips about how I could handle IO better, or if possible run some of my things and see how long they take on SPOJ, just to see how far off I am?
Here's how my IO works on, for example, WATER:
readInt :: BS.ByteString -> Int
readInt x =
case BS.readInt x of Just (i,_) -> i
Nothing -> error "Unparsable Int"
--Parser: takes k lines, and puts them into a grid
parseGrid :: [BS.ByteString] -> Grid
parseGrid s = (map (map readInt)).(map BS.words) $ s
--Parser. Input should be (a string of) integers, separated by spaces.
parseInt :: String -> [Int]
parseInt = (map read).words
printAnswer :: Int -> [BS.ByteString] -> IO()
printAnswer 0 _ = return()
printAnswer n (x:xs) = do
let (a:b:stuff) = (map readInt).BS.words $ x
let s = take a xs
rest = drop a xs
l = parseGrid s
print $ answer' (a,b,l)
printAnswer (n-1) rest
main :: IO()
main = do
[n] <- (fmap parseInt $ getLine)
x <- (fmap BS.lines) BS.getContents
printAnswer n x
Please be gentle, I just started out in Haskell! Sorry to be a hassle.
Thanks.
created
last reply
- 2
replies
- 822
views
- 2
users
- 2
links