[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 ...
随机推荐
- PPTP-VPN第一章——部署与简单使用
最近前游戏同事搞了台的VPS,贡献出来做VPN使用.目前流行的服务器VPN软件主要有PPTP VPN和Openvpn.Openvpn虽然功能较为强大,且安全性高,支持nat穿越等等,但服务器端和客户端 ...
- [Swift] 疑难杂症
[Swift] 疑难杂症 1.class .... has no initializers --> class 的每一个元素都需要初始化,否则会报错,除了可空元素
- HttpTool.java(在java tool util工具类中已存在) 暂保留
HttpTool.java 该类为java源生态的http 请求工具,不依赖第三方jar包 ,即插即用. package kingtool; import java.io.BufferedReader ...
- 关于js闭包的误区
一直以为js的闭包只是内部函数保存了一份外部函数的变量值副本,但是以下代码打破了我的认识: function createFunctions() { var result = new Array(); ...
- BestCoder Round #90 //div all 大混战 一题滚粗 阶梯博弈,树状数组,高斯消元
BestCoder Round #90 本次至少暴露出三个知识点爆炸.... A. zz题 按题意copy Init函数 然后统计就ok B. 博弈 题 不懂 推了半天的SG..... 结果这 ...
- linux服务器TCP并发连接数优化
1.查看用户单一进程最大文件打开数 [root@localhost ~]# ulimit -n 1024 2.修改/etc/security/limits.conf文件,添加下面两行, [root@l ...
- JS鼠标移入,移出事件
该事件的效果就像百度首页的设置选项,当鼠标移入,移出时的效果,废话不多说了,直接上码. <!DOCTYPE html><html lang="en">< ...
- JAVA学习笔记(二):eclipse智能提示(转)
存盘 Ctrl+s(肯定知道)注释代码 Ctrl+/取消注释 Ctrl+\(Eclipse3已经都合并到Ctrl+/了)代码辅助 Alt+/快速修复 Ctrl+1代码格式化 Ctrl+Shift+f整 ...
- sql删除前导和后缀
1.patindex用法 patindex('%pattern%', expression) pattern--> 正则表达式,需要匹配的前导内容,可以进通配: expression--> ...
- PHP使用命名空间:别名/导入(Aliasing/Importing)
1.导入,就是使用use操作符 2.在一个类中导入了另一个类之后,当前的命名空间仍然是当前类的命名空间 3.注意对命名空间中的名称(包含命名空间分隔符的完全限定名称如 Foo\Bar以及相对的不包含命 ...