Problem Introduction

An inversion of a sequence \(a_0,a_1,\cdots,a_{n-1}\) is a pair of indices \(0 \leq i < j < n\) such that \(a_i>a_j\). The number of inversions of a sequence in some sense measures how close the sequence is to being sorted. For example, a sorted(in non-descending order) sequence contains no inversions at all, while in a sequence sorted in descending order any two elements constitute an inversion (for a total of \(n(n-1)/2\) inversions).

Problem Description

Task.The goal in this problem is to count the number of inversions of a given sequence.

Input Format.The first line contains an integer \(n\), the next one contains a sequence of integers \(a_0,a_1,\cdots,a_{n-1}\)

Constraints.\(1 \leq n \leq 10^5, 1 \leq a_i \leq 10^9\) for all \(0 \leq i < n\).

Output Format.Output the number of inversions in the sequence.

Sample 1.
Input:

5
2 3 9 2 9

Output:

2

Solution

# Uses python3
import sys

def merge_and_count(a, b):
    c = []
    number_of_inversions = 0
    i = j = 0
    while i < len(a) and j < len(b):
        if a[i] <= b[j]:
            c.append(a[i]); i += 1
        else:
            c.append(b[j]); j += 1
            number_of_inversions += len(a)-i
    while i < len(a):
        c.append(a[i]); i += 1
    while j < len(b):
        c.append(b[j]); j += 1
    return c, number_of_inversions

def get_number_of_inversions(a, b, left, right):
    number_of_inversions = 0
    if right - left <= 1:
        return number_of_inversions
    ave = (left + right) // 2
    number_of_inversions += get_number_of_inversions(a, b, left, ave)
    number_of_inversions += get_number_of_inversions(a, b, ave, right)
    b, count = merge_and_count(a[left:ave], a[ave:right])
    a[left:right] = b
    return number_of_inversions + count

if __name__ == '__main__':
    input = sys.stdin.read()
    n, *a = list(map(int, input.split()))
    b = n * [0]
    print(get_number_of_inversions(a, b, 0, len(a)))

[UCSD白板题] Number of Inversions的更多相关文章

  1. [UCSD白板题] The Last Digit of a Large Fibonacci Number

    Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...

  2. [UCSD白板题 ]Small Fibonacci Number

    Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...

  3. [UCSD白板题] Huge Fibonacci Number modulo m

    Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...

  4. [UCSD白板题] Compute the Edit Distance Between Two Strings

    Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...

  5. [UCSD白板题] Take as Much Gold as Possible

    Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...

  6. [UCSD白板题] Primitive Calculator

    Problem Introduction You are given a primitive calculator that can perform the following three opera ...

  7. [UCSD白板题] Points and Segments

    Problem Introduction The goal in this problem is given a set of segments on a line and a set of poin ...

  8. [UCSD白板题] Pairwise Distinct Summands

    Problem Introduction This is an example of a problem where a subproblem of the corresponding greedy ...

  9. [UCSD白板题] Covering Segments by Points

    Problem Introduction You are given a set of segments on a line and your goal is to mark as few point ...

随机推荐

  1. linq andregex

  2. ubuntu 安装mysql-python和 python-ldap,navicate 问题

    1.Ubuntu 下 pip install mysql-python 报错 EnvironmentError: mysql_config not found 原因是缺少mysqlclient 包,执 ...

  3. C# 解析json

    在接口开发的过程中经常通过接口获取数据返回是json格式字符串. 但是返回的字符串可能比较复杂,可能不止一种类型的数据. 例如: { "resultCode": "0&q ...

  4. 启动项目时,报错;Address already in use: JVM_Bind<null>:8080

    Address already in use: JVM_Bind<null>:8080在MyEclipse启动或者是tomcat启动的时候出现:Address already in use ...

  5. 直接用<img> 的src属性显示base64转码后的字符串成图片

    直接用<img> 的src属性显示base64转码后的字符串成图片 <img src="base64转码后的字符串" ></img> 下面的图片 ...

  6. B. Shaass and Bookshelf DP

    http://codeforces.com/contest/294/problem/B 据说是贪心,我用了一个复杂度是2e8的dp水过去了. 其实这题就是给你n个数,每个数有两个权值,分成两组,使得第 ...

  7. 调用JavaScript

    当webdriver 遇到没法完成的操作时,笔者可以考虑借用JavaScript 来完成,比下下面的例子,通过JavaScript 来隐藏页面上的元素.除了完成webdriver 无法完成的操作,如果 ...

  8. 利用Native Client OLEDB 11 高效率地对SQL SERVER 进行查询和插入操作

    前言: 鄙司原始用的都是ADO来访问数据库,而我现在着手的项目是从我的GPS历史数据库中,取出历时数据的一个接口,一个DLL.用ADO写完之后,测试下来,平均4000条的数据,需要 180 毫秒左右. ...

  9. 嵌入式linux学习笔记1—内存管理MMU之虚拟地址到物理地址的转化

    一.内存管理基本知识 1.S3C2440最多会用到两级页表:以段的方式进行转换时只用到一级页表,以页的方式进行转换时用到两级页表.页的大小有三种:大页(64KB),小页(4KB),极小页(1KB).条 ...

  10. vim--macro

    例: qa some vim command q 这个宏只记录了vim命令到寄存器a中,执行这个宏可以用命令: @a 也可以加上执行次数: 10@a 执行10次 当你执行过一次@a之后,你可以用 @@ ...