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. error C4996: 'fopen': This function or variable may be unsafe.

    vs2013中错误提示信息: error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s ...

  2. SSH的端口转发:本地转发Local Forward和远程转发Remote Forward

    关于使用ssh portforwarding来进行FQ的操作,网络上已经有很多很好的文章,我在这里只是画两个图解释一下. 首先要记住一件事情就是: SSH 端口转发自然需要 SSH 连接,而 SSH ...

  3. codeforces 742E (二分图着色)

    E. Arpa's overnight party and Mehrdad's silent entering Note that girls in Arpa's land are really at ...

  4. C#实现简单的委托异步调用

    delegate void textAsy(); static void Main(string[] args) { textAsy t = texts; AsyncCallback callBack ...

  5. 发送短信MFMessageComposeViewController

    if([MFMessageComposeViewController canSendText]) { MFMessageComposeViewController * controller = [[M ...

  6. php开发环境搭建——laravel框架,apache服务器,git版本控制

    本文主要阐述做项目前的开发环境安装——后端为php,前端采用grunt进行自动化构建.具体介绍了windows平台下采用apache运行php的环境搭建,以及git工具安装.写得有点粗糙,但过程完整, ...

  7. LTS

    LTS(light-task-scheduler)主要用于解决分布式任务调度问题,支持实时任务,定时任务和Cron任务.有较好的伸缩性,扩展性,健壮稳定性而被多家公司使用. 1. 支持分布式,解决多点 ...

  8. command line

    command line terminal vim 编辑工具 vim 编辑命令 j 光标上移 k 光标下移 l 光标左移 h 光标右移 x / dd 删除一行 v 多行模式 :w 保存 :q 不保存退 ...

  9. codeforces 446C DZY Loves Fibonacci Numbers 线段树

    假如F[1] = a, F[2] = B, F[n] = F[n - 1] + F[n - 2]. 写成矩阵表示形式可以很快发现F[n] = f[n - 1] * b + f[n - 2] * a. ...

  10. Java Struts2 POI创建Excel文件并实现文件下载

    Java Struts2 POI创建Excel文件并实现文件下载2013-09-04 18:53 6059人阅读 评论(1) 收藏 举报 分类: Java EE(49) Struts(6) 版权声明: ...