Skip to content

Instantly share code, notes, and snippets.

@tanakh
Created December 3, 2013 05:04
Show Gist options
  • Save tanakh/7764127 to your computer and use it in GitHub Desktop.
Save tanakh/7764127 to your computer and use it in GitHub Desktop.
新人女子にHaskellを教え込むHaskellerの鑑 https://paiza.jp/poh/ec-campaign あなたの部署に配属された新人女子プログラマの野田さんのコードをより良いものに直してください。野田さんは実はあなたの会社の社長令嬢。効率の良いコードに書き換えて、プログラマとしてのスキルをアピールできれば昇進するチャンスです。
{-# LANGUAGE BangPatterns #-}
import Control.Monad
import Control.Monad.ST
import Control.Applicative
import qualified Data.Vector.Unboxed as V
import Data.Vector.Unboxed ((!))
import qualified Data.Vector.Algorithms.Intro as Intro
isort v = runST $ do
mv <- V.thaw v
Intro.sort mv
V.freeze mv
main :: IO ()
main = do
[n, d] <- map read . words <$> getLine
ps <- isort . V.fromList <$> replicateM n readLn
replicateM_ d $ do
m <- readLn
let f i j !acc
| i < j =
let cur = ps ! i + ps ! j in
if cur <= (m :: Int)
then f (i+1) j (max acc cur)
else f i (j-1) acc
| otherwise =
acc
print $ f 0 (V.length ps - 1) 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment