[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 ...
随机推荐
- eclipse maven spring mvc el表达式无效
http://www.myexception.cn/javascript/2031310.html
- html初学者笔记01
一.Html简介 HTML 是一种标记语言 忽略大小写,语法宽松 使用 HTML 标记和元素,可以: 控制页面和内容的外观 发布联机文档 使用 HTML 文档中插入的链接检索联机信息 创建联机表单,收 ...
- html初学者了解的笔记02
一.Html简介 HTML 是一种标记语言 忽略大小写,语法宽松 使用 HTML 标记和元素,可以: 控制页面和内容的外观 发布联机文档 使用 HTML 文档中插入的链接检索联机信息 创建联机表单,收 ...
- debug命令简介
debug命令不区分大小,debug的命令都是一个字母,后跟或不跟参数 1.debug [路径\文件] [参数] [参数]--[参数] debug相应程序 2. D(Dump) [地址] [范围] 显 ...
- yii2 pjax使用
Pjax::begin(['id'=>'datalist']); echo GridView::widget([ 'dataProvider' => $dataProvider, 'col ...
- Volley简单封装
public interface IRequest { /** * 获取头部信息 * * @return */ public Map<String, String> getHeaderMa ...
- Convert.ToInt32,int.Parse,int.TryParse,(int)的区别
1 (int)变量名[强制类型转换] 该转换方式主要用于数字类型转换,从int类型到long,float,double,decimal类型,可以使用隐式转换,但是从long类型到int类型就需要使用显 ...
- 判断当前VC是不是正在展示的活跃页面
viewController.isViewLoaded && viewController.view.window
- Reverse链表 非递归实现
#include<iostream> struct node{ int payload; node* next; }; void bianli(node* head){ node* ite ...
- EasyUI中Treegrid节点的删除
// 删除function removes() { var rows = ruletreegrid.treegrid('getSelections'); if (rows &&am ...