[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 ...
随机推荐
- C# 模拟键盘操作--SendKey(),SendKeys()
模拟键盘输入就是使用以下2个语法实现的.SendKeys.Send(string keys); //模拟汉字(文本)输入SendKeys.SendWait(string keys); //模拟按键输 ...
- ubuntu远程桌面连接windows系统(转)
现在用ubuntu系统,公司买了个windows的服务器,需要给配置一套环境,来回跑很麻烦,就想windows下可以的远程桌面,linux应该也有. 现在自己的ubuntu13.10,无法进入桌面的& ...
- 第一次将内容添加到azure event hubs
由于每秒数据吞吐量巨大,需要将实时数据存到event hubs,再由event hubs定时定量保存到document DB. event hubs的介绍详见微软官页:https://azure.mi ...
- linux用户管理
管理用户的文件 添加用户组 添加用户 修改权限
- oracle启动脚本 .
.#!/bin/bash set -x su -oracle >>EON lsnrctl start sqlplus /nolog >>EOF conn / as sy ...
- Spring事务管理(转)
1 初步理解 理解事务之前,先讲一个你日常生活中最常干的事:取钱. 比如你去ATM机取1000块钱,大体有两个步骤:首先输入密码金额,银行卡扣掉1000元钱:然后ATM出1000元钱.这两个步骤必须是 ...
- WinForm richtextbox 关键字变红色
private void HilightRichText(RichTextBox control, string hilightString) { int nSel ...
- CentOS 6.5安装 ASM lib
asmlib针对linux centos 6.5版本包,包括内核升级包.下载失败的话自己配置网易yum源. 安装方法: yum install oracleasm 执行后,会检查环境依赖情况,包括内核 ...
- poj -- 1042 Gone Fishing(枚举+贪心)
题意: John现有h个小时的空闲时间,他打算去钓鱼.钓鱼的地方共有n个湖,所有的湖沿着一条单向路顺序排列(John每在一个湖钓完鱼后,他只能走到下一个湖继续钓),John必须从1号湖开始钓起,但是他 ...
- 基于webmagic的爬虫小应用--爬取知乎用户信息
听到“爬虫”,是不是第一时间想到Python/php ? 多少想玩爬虫的Java学习者就因为语言不通而止步.Java是真的不能做爬虫吗? 当然不是. 只不过python的3行代码能解决的问题,而Jav ...