用归并排序方式

最原始的方法的复杂度是O(n^2)。

使用归并排序的方式,可以把复杂度降低到O(nlgn).

设A[1..n]是一个包含N个非负整数的数组。如果在i〈 j的情况下,有A〉A[j],则(i,j)就称为A中的一个逆序对。
例如,数组(3,1,4,5,2)的“逆序对”有<3,1>,<3,2><4,2><5,2>,共4个。

注:我觉得方法是在归并过程中,记录每次归并,后面的分组中被放到了前面的元素的个数。

的确是这样的。下面这个也是MergeSort的基本算法框架:

 int gCount = ;

  template<class Iterator>int merge(Iterator begin, Iterator mid, Iterator end)

  {
Iterator iL = begin;
Iterator iR = mid; int count = distance(begin, end);
vector<int> v(count);
vector<int>::iterator it = v.begin(); while(iL != mid && iR != end)
{
if(*iL <= * iR)
{
*it++ = *iL++;
}
else
{
gCount += distance(iL, mid);
*it++ = *iR++;
}
} if(iL == mid) copy(iR, end, it);
if(iR == end) copy(iL, mid, it); copy(v.begin(), v.end(), begin); return ; } template<class Iterator>int mergeSort(Iterator begin, Iterator end) {
int count, step;
count = distance(begin, end); if(count <= )
{
return ;
} step = count / ; mergeSort(begin, begin + step);
mergeSort(begin + step, end); merge(begin, begin + step, end); return ; }

重点在 gCount += distance(iL, mid) (注:distance其实就是位置的减法)

逆序对数实质就是插入排序过程中要移动元素的次数。

要移动的元素的位数即第一个序列中还未插入到新序列中的元素的个数

即: distance(iL, mid)

求逆序对数总结 & 归并排序的更多相关文章

  1. 归并排序(归并排序求逆序对数)--16--归并排序--Leetcode面试题51.数组中的逆序对

    面试题51. 数组中的逆序对 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数. 示例 1: 输入: [7,5,6,4] 输出 ...

  2. hdu 4911 求逆序对数+树状数组

    http://acm.hdu.edu.cn/showproblem.php?pid=4911 给定一个序列,有k次机会交换相邻两个位置的数,问说最后序列的逆序对数最少为多少. 实际上每交换一次能且只能 ...

  3. poj 2299 树状数组求逆序对数+离散化

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 54883   Accepted: 20184 ...

  4. 算法笔记_065:分治法求逆序对(Java)

    目录 1 问题描述 2 解决方案 2.1 蛮力法 2.2 分治法(归并排序)   1 问题描述 给定一个随机数数组,求取这个数组中的逆序对总个数.要求时间效率尽可能高. 那么,何为逆序对? 引用自百度 ...

  5. codeforces 459D D. Pashmak and Parmida's problem(离散化+线段树或树状数组求逆序对)

    题目链接: D. Pashmak and Parmida's problem time limit per test 3 seconds memory limit per test 256 megab ...

  6. java实现求逆序对

    1 问题描述 给定一个随机数数组,求取这个数组中的逆序对总个数.要求时间效率尽可能高. 那么,何为逆序对? 引用自百度百科: 设 A 为一个有 n 个数字的有序集 (n>1),其中所有数字各不相 ...

  7. codeforces 540E 离散化技巧+线段树/树状数组求逆序对

    传送门:https://codeforces.com/contest/540/problem/E 题意: 有一段无限长的序列,有n次交换,每次将u位置的元素和v位置的元素交换,问n次交换后这个序列的逆 ...

  8. HDU 3743 Frosh Week(归并排序求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3743 题目意思就是给你一个长为n的序列,让你求逆序对.我用的是归并排序来求的.归并排序有一个合并的过程 ...

  9. 浙江工商大学15年校赛I题 Inversion 【归并排序求逆序对】

    Inversion Time Limit 1s Memory Limit 131072KB Judge Program Standard Ratio(Solve/Submit) 15.00%(3/20 ...

随机推荐

  1. CF995E Number Clicker 解题报告

    CF995E Number Clicker 题目描述 Allen is playing Number Clicker on his phone. He starts with an integer u ...

  2. 刷题总结——Interval query(hdu4343倍增+贪心)

    题目: Problem Description This is a very simple question. There are N intervals in number axis, and M ...

  3. hdu 4388 Stone Game II sg函数 博弈

    Stone Game II comes. It needs two players to play this game. There are some piles of stones on the d ...

  4. eclipse搭建ssm框架的maven的工程

    版本:eclipse:Indigo Service Release 2.  jdk :jdk1.7.0_03. maven:apache-maven-3.3.3 . 上面的3个东西 先下载下来.然后运 ...

  5. WeiXin 验证成为开发者和更换服务器验证代码

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Da ...

  6. C++ delete 两次

    转载自:http://blog.csdn.net/jxluofeng/article/details/19766801 <问题>危险的代码: int* p=new int(1);   de ...

  7. OpenGL “太阳、地球和月亮”天体运动动画 例子

    http://oulehui.blog.163.com/blog/static/7961469820119186616743/ OpenGL “太阳.地球和月亮”天体运动动画 例子 2011-10-1 ...

  8. 【leetcode】500. Keyboard Row

    问题描述: Given a List of words, return the words that can be typed using letters of alphabet on only on ...

  9. android studio 无法调试debug,(能运行安装)

    请检查清单文件的改为true就可以调试了 android:debuggable="true"

  10. 高级列表控件ListCtrl关联的MFC中的类:CListCtrl

    高级列表控件ListCtrl关联的MFC中的类:CListCtrl■ 报表样式ListCtrl常用操作:1.添加列标题头:InsertColumn2.获取与设置列宽:GetColumnWidth.Se ...