[UCSD白板题] Number of Inversions
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的更多相关文章
- [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_ ...
- [UCSD白板题 ]Small Fibonacci Number
Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...
- [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_ ...
- [UCSD白板题] Compute the Edit Distance Between Two Strings
Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...
- [UCSD白板题] Take as Much Gold as Possible
Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...
- [UCSD白板题] Primitive Calculator
Problem Introduction You are given a primitive calculator that can perform the following three opera ...
- [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 ...
- [UCSD白板题] Pairwise Distinct Summands
Problem Introduction This is an example of a problem where a subproblem of the corresponding greedy ...
- [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 ...
随机推荐
- Tcc学习笔记(三) 使用举例
TCC的使用以使用第三方库为例子,例子包括:OpenGL , GMP以及SDL等. 1.TCC使用GLUT 去OpenGL下载http://www.opengl.org/resources/libra ...
- 配置FastReport,FastReport报表加载不出来
插件链接: Demo地址:http://pan.baidu.com/s/1dEXUvsP FastReport.Net软件地址:https://pan.baidu.com/s/1c2kNBVi ...
- 经典的找不到符号(symbol)错误 #iOS开发
使用BmobSDK的过程中,编译时出现了以下错误信息,意思是 -[BmobSRWebSocket _innerPumpScanner] 这个方法引用了 "_utf8_nextCharSafe ...
- 记一次u盘 无法格式化、0字节、写保护的解决过程
首先各种找方法,下载了一堆烂七八糟的东西都没能解决 后来看了这个链接的文章 http://jingyan.baidu.com/article/6079ad0e5bdec428ff86dbcd.html ...
- CODE[VS] 1230 元素查找
1.题目戳这里 2.代码: #include<iostream> #include<algorithm> using namespace std; int n,m,a[1000 ...
- js 阻止浏览器默认行为
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 如何用java获得字符串的ASCII值
使用Integer.valueOf就可以直接将char类型的数据转为十进制数据表现形式. int value=Integer.valueOf('1');//49int value=Integer.va ...
- Android开发工具全面转向Android Studio(2)——AS project/module的CRUD
本文有些地方可能需要衔接Android开发工具全面转向Android Studio(1)——准备开发环境,读起来效果会更好. 这个世界很奇妙,所有的东西离不开CRUD,即增删改查.即使人本身也遵循这个 ...
- STL学习笔记
简介 STL(Standard Template Library),即标准模版库,涵盖了常用的数据结构和算法,并具有跨平台的特点.STL是C++标准函数库的一部分,如下图所示: STL含有容器.算法和 ...
- 在CMMI推广过程中EPG常犯的错误(转)
本文转自: http://developer.51cto.com/art/200807/86953.htm 仅用于个人收藏,学习.如有转载,请联系原作者. ---------------------- ...