Created
January 23, 2011 10:15
-
-
Save anonymous/791960 to your computer and use it in GitHub Desktop.
Just brute force, but it works
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Text.Printf | |
-- Should be in some library | |
blockBy _ [] = [] | |
blockBy n xs = ys : blockBy n zs where (ys, zs) = splitAt n xs | |
main = interact $ | |
unlines . | |
zipWith (printf "Case #%d: %.0f") [1::Int ..] . | |
map (\ [[r,k,_], xs] -> solve r k xs) . | |
blockBy 2 . | |
map (map read . words) . | |
tail . | |
lines | |
solve rounds seats xs = loop 0 rounds maxGroup seats (cycle xs) | |
where maxGroup = length xs | |
loop :: Double -> Int -> Int -> Int -> [Int] -> Double | |
loop t 0 _ _ _ = t | |
loop t r 0 _ q = loop t (r-1) maxGroup seats q | |
loop !t !r !g !k (x:q) | x <= k = loop (t+fromIntegral x) r (g-1) (k-x) q | |
| otherwise = loop t (r-1) maxGroup seats (x:q) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment