fork(5) download
  1. def inv_count(a, m=(1<<32)):
  2. if not m or not a:
  3. return 0
  4. count = 0
  5. ones = []
  6. zeros = []
  7. for n in a:
  8. if n & m:
  9. ones.append(n & ~m)
  10. else:
  11. count += len(ones)
  12. zeros.append(n & ~m)
  13. m /= 2
  14. return count + inv_count(ones, m) + inv_count(zeros, m)
  15.  
  16.  
  17. print inv_count([1, 2, 3, 4, 5])
  18. print inv_count([5, 4, 3, 2, 1])
Success #stdin #stdout 0.01s 7692KB
stdin
Standard input is empty
stdout
0
10