Created
April 12, 2011 15:37
-
-
Save oskimura/915740 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| {-# LANGUAGE CPP #-} | |
| {-# OPTIONS_GHC -O2 -optc-O3 -optc-ffast-math#-} | |
| {-# OPTIONS_GHC -funbox-strict-fields -fexcess-precision -monly-3-regs #-} | |
| {-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-} | |
| {-# LANGUAGE NoMonomorphismRestriction #-} | |
| module Main where | |
| import Control.Applicative | |
| import Data.List | |
| import Data.Function (on) | |
| import Text.Printf | |
| import Control.Monad | |
| import Data.Tuple (swap) | |
| import Data.Array | |
| import qualified Data.Map | |
| import Text.XFormat.Read | |
| --import Control.Monad.State | |
| import Data.Maybe | |
| splitBy :: (a -> Bool) -> [a] -> [[a]] | |
| splitBy p [] = [] | |
| splitBy p xs = a : splitBy p (dropWhile p b) | |
| where | |
| (a, b) = break p xs | |
| split :: String -> [String] | |
| split = splitBy (==' ') | |
| rr :: String -> Integer | |
| rr = read | |
| --input = lines <$> getContents | |
| --input = lines <$> readFile "/Users/oskimura/prog/input.txt" | |
| --input = lines <$> readFile "/Users/oskimura/Downloads/A-small-practice (2).in" | |
| input = lines <$> readFile "/Users/oskimura/Downloads/A-large-practice (1).in" | |
| slices = unfoldr . phi | |
| where phi n xs | n < 1 || null xs = Nothing | |
| | otherwise = Just $ splitAt n xs | |
| toIS text = case fromJust . readf (Integer % String) $ text of | |
| x :%: b -> (x,b) | |
| output xs = unlines . zipWith (\ c n -> "Case #"++ show c ++": "++ show n) [1..] $ xs | |
| put = writeFile "output.txt" | |
| f = foldr (\(x,y) (as,bs) -> if y=="R" then (x:as,bs) else (as,x:bs) ) ([],[]) | |
| g rs bs = sum' - (len*2) | |
| where sum' = sum ns | |
| ns = zipWith (+) (bigBy rs) $ bigBy bs | |
| len = genericLength ns | |
| bigBy = reverse . sort | |
| main = do {count : nums <- map words <$> input | |
| ; let ns = map (rr . head . head) . slices 2 $ nums | |
| ; let bs = map f . map (map toIS) . concatMap tail . slices 2 $ nums in | |
| put . output . map (uncurry g) $ bs | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment