Last active
June 17, 2016 20:29
-
-
Save josejuan/c04025b3c618c919144347b4e70f2cc8 to your computer and use it in GitHub Desktop.
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 System.Environment | |
import qualified Data.Set as Set | |
{- | |
For each x input element | |
Split current set on `x` O(log n) | |
Count right side O(1) | |
Add x to set O(log n) | |
-} | |
inv :: Ord a =>[a] ->Int | |
inv = fst . foldl (\(n, s) x ->(n + (S.size . snd . S.split x) s, S.insert x s)) (0, S.empty) | |
main = getContents >>= print . (inversions :: [Int] ->Int) . map read . words | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment