Difference between revisions of "Google Code Jam/Text Messaging Outrage"

From HaskellWiki
Jump to: navigation, search
(Deleting page that hasn't been edited for over 10 years)
m (Reverted edits by Tomjaguarpaw (talk) to last revision by Mnislaih)
 
Line 1: Line 1:
 +
== Solution ==
 +
<haskell>
  
 +
main = (enumFromTo (1::Int) <$> readLn) >>= mapM_ go
 +
  where go i = do
 +
          [p,k,l] <- map read . words <$> getLine
 +
          nn <- map read . words <$> getLine
 +
          printf "Case #%i: %i\n" i (solve k nn)
 +
 +
solve :: Int -> [Integer] -> Integer
 +
solve k l = let rounds = map sum $ chunks k $ sortBy (flip compare) l
 +
              in sum $ zipWith (*) [1..] rounds
 +
chunks n [] = []
 +
chunks n as = bs : chunks n cs where (bs,cs) = splitAt n as
 +
 +
</haskell>

Latest revision as of 15:18, 6 February 2021

Solution

main = (enumFromTo (1::Int) <$> readLn) >>= mapM_ go
  where go i = do
          [p,k,l] <- map read . words <$> getLine
          nn <- map read . words <$> getLine
          printf "Case #%i: %i\n" i (solve k nn)

solve :: Int -> [Integer] -> Integer
solve k l = let rounds = map sum $ chunks k $ sortBy (flip compare) l
              in sum $ zipWith (*) [1..] rounds
chunks n [] = []
chunks n as = bs : chunks n cs where (bs,cs) = splitAt n as