萌新求助,Haskell 不开 O2 AC,开 O2 30分
查看原帖
萌新求助,Haskell 不开 O2 AC,开 O2 30分
132435
智子楼主2021/8/23 11:06

代码如下:

zeroOnePack :: [(Int, Int)] -> Int -> Int
zeroOnePack items weight = head (dp items weight) where
    dp :: [(Int, Int)] -> Int -> [Int]
    dp [] 0 = [0]
    dp (_:items) 0 = 0:dp items weight
    dp [] m = 0:dp [] (m - 1)
    dp ((w, v):items) m =
        let f = dp ((w, v):items) (m - 1) in
            if w <= m then
                max (f !! weight) (f !! (weight + w) + v) : f
            else
                (f !! weight) : f
                
parse :: String -> (Int, Int)
parse s =
    let tmp = (map read . words) s
    in (head tmp, last tmp)

main = do
    [weight, number] <- map read . words <$> getLine
    input <- getContents
    let items = map parse (lines input)
    print (zeroOnePack items weight)

不开 O2 AC

加上 O2 优化后

{-# OPTIONS_GHC -O2 #-}

开 O2 30分

想知道是代码里什么问题导致了 GHC 负优化……

2021/8/23 11:06
加载中...