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. jsp 访问jdbc实例

    前提条件: 1.安装Java,tomcat,MySQL,eclipse 2.配置java 环境变量 3.配置MySQL参数 4.tomcat能正常启动 使用eclipse编写代码: 1. 新建一个we ...

  2. vs2010边调试边编辑后台.cs文件的办法

    方法一:在web项目的属性页里的web标签页,选中“启用编辑并继续”项 方法二:菜单 工具+选项+调试+编辑并继续,选中“启用编辑并继续”项. 设置完之后,调试web项目的时候可以直接修改.cs文件, ...

  3. scrollview背景头部拉伸

    a - (void)viewDidLoad { [super viewDidLoad]; self.tableView.contentInset = UIEdgeInsetsMake(kImageOr ...

  4. Postgresql死锁处理

    今天遇到Postgresql的一个问题,部分表记录的update一直无效报错,初步判断为锁表,赶紧进行解决. 1. 查询死锁进程列表 select * from pg_stat_activity wh ...

  5. spring的事物实现

    Spring的事物主要有三个接口 PlatformTransactionManager. 根据TransactionDefinition配置的事物信息创建事物 TransactionDefinitio ...

  6. thinkphp 数据访问

    <?php namespace Admin\Controller; use Think\Controller; class MainController extends Controller { ...

  7. IOS低版本遇到了坑不知道你遇到了没

    拿着项目给客户测试,客户那边三个人俩人水果手机是ios8以下版本,结果导致```(恭喜,坑出现!)总不能说老总!"您把版本升级到ios9 吧!

  8. Eclipse之Git提交项目

    一.使用eclipse自带插件提交项目 1.自带git插件进行配置我们的用户名和密码,即是自己github注册的用户. windows-perferences-Team-Git-configurati ...

  9. thinkpad e450 win7黑苹果macos 10.10.5(网/显/声卡驱动)安装成功

    首先上图: 过程: 1.使用变色龙安装macos 10.10.5懒人版黑苹果 2.使用Haswell破解内核替换,成功进入系统 2.5.使用Hackintosh Vietnam Tool 1.9.6以 ...

  10. IIS 7 的 500 內部錯誤

    印象中這個有名的錯誤提示從 ASP 時代就已經存在 不是很明白微軟怎麼想的 你們所面對的客戶族群有為數不少的開發人員 除錯環境對開發者來說算是基本配備 為何不把這些相關設定做成「出廠預設值」? 都幾年 ...