Skip to content

Instantly share code, notes, and snippets.

@josejuan
Last active June 17, 2016 20:29
Show Gist options
  • Save josejuan/c04025b3c618c919144347b4e70f2cc8 to your computer and use it in GitHub Desktop.
Save josejuan/c04025b3c618c919144347b4e70f2cc8 to your computer and use it in GitHub Desktop.
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