逆序对数问题Count Inversion

Problem Description
Recall the problem of finding the number of inversions. As in the course, we are given a sequence of n numbers a1,··· ,an, which we assume are all distinct, and we define an inversion to be a pair i < j such that ai > aj.
We motivated the problem of counting inversions as a good measure of how different two orderings are. However, one might feel that this measure is too sensitive. Let’s call a pair a significant inversion if i < j and ai > 3aj. Given an O(nlogn) algorithm to count the number of significant inversions between two orderings.
The array contains N elements (1<=N<=100,000). All elements are in the range from 1 to 1,000,000,000.
回想一下找到反转数的问题。 就像在过程中一样,我们给定n个数字a1,…,an的序列,我们假设它们是完全不同的,并且我们将一个反转定义为对i <j,从而ai> aj。
我们提出了将反演计数的问题,以此作为衡量两种排序的不同程度的一种好方法。 但是,人们可能会认为此措施过于敏感。 如果i <j和ai> 3aj,我们将这对称为显着倒置。 给定一个O(nlogn)算法来计算两个排序之间的有效反转次数。该数组包含N个元素(1 <= N <= 100,000)。
所有元素的范围都在1到1,000,000,000之间。
Input
The first line contains one integer N, indicating the size of the array. The second line contains N elements in the array.
50% test cases guarantee that N < 1000.
Output
Output a single integer which is the number of pairs of significant inversions.
Sample Inout
6
13 8 5 3 2 1
Sample Output
6

算法思想

采用分治法与归并排序思想,不停地将序列A分割为两个等长的子序列 L 和 R ,分别对 L 和 R 中的significant inversions进行计数,然后对 L 和 R 组成的总序列A中所有的significant inversions 进行合并计数,A中的总计数为 L 和 R 中的分别计数加上合并后而引起的新计数。

算法伪代码

Sort-and-Count(A)
Diride A into two sub-sequence L and R
(RCL,L) = Sort-and-Count(L)
(RCR,R) = Sort-and-Count(R)
(r, A) = Merge-and-Count(L, R)
return (RC = RCL + RCR + r,A)Merge-and-Count(L, R)
InverseCount = 0
i = 1, j = 1
for k = 1 to r doif L[i] > R[j] thenA[k] = L[j]j++elseA[k] = R[i]i++end if
end for
i = 1,j = 1
for k = 1 to r doif L[i] > 3R[j] then InverseCount += length - i + 1++jelse ++iend if
end for
return InverseCount and A

正确性证明

对于每个i,我们都计算 ai 到 aj 的有效反转次数,如果 ai <= 3aj ,则 aj 与任何 am (m>=j)之间都没有有效的反转次数,因此我们减少 j ,如果 ai >= 3aj ,则对于所有的 m (k<m<=j) 都有 ai >= 3am ,因此我们已经检验到涉及 ai 的 j - k 个显著的反演了。

复杂度分析

Merge-and-Count中的每个for循环都最多执行 n 次,故时间复杂度为 O(n) ,根据二分算法的特性,最多分割logn次,算法最多执行logn次,所以总的时间复杂度为O(nlogn)。

源代码

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>using namespace std;const int maxn = 1e5 + 10;
const int MAX = 1e9 + 1;
int a[maxn];
map<int, int> m;int lowbit(int x) {return x & -x;
}void add(int x) {while (x < MAX) {m[x]++;x += lowbit(x);}
}int sum(int x) {int res = 0;while (x) {res += m[x];x -= lowbit(x);}return res;
}int main() {int n; cin >> n;for (int i = 0; i < n; i++) cin >> a[i];long long res = 0;for (int i = n - 1; i >= 0; i--) {res += sum((a[i] - 1) / 3);add(a[i]);}cout << res << endl;return 0;
}

Count Inversion逆序对数问题相关推荐

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

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

  2. 蓝桥杯-逆序对(java)

    算法训练 逆序对   时间限制:1.0s   内存限制:256.0MB 问题描述 Alice是一个让人非常愉跃的人!他总是去学习一些他不懂的问题,然后再想出许多稀奇古怪的题目.这几天,Alice又沉浸 ...

  3. 逆序对java_逆序对

    求逆序对问题用归并排序的时间复杂度比暴力算法更低. 假设有一个数组{8,1,2,5,7,4,3,6} 首先归并排序第一次对数组进行分割      8 1 2 5      7 4 3 6 二次分割  ...

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

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

  5. 求逆序对数目(合并排序)

    7-2 求逆序对数目 (30分) 注意:本问题算法的时间复杂度要求为O(nlogn), 否则得分无效 题目来源:http://poj.org/problem?id=1804 Background Ra ...

  6. 归并排序求逆序对(C语言)

    目录 1.归并排序过程 (1)逆序对概念 (2)基本思想 (3)算法实现 1.归并排序过程 使用归并排序实现求逆序对,那么首先得了解什么是归并排序(关于归并排序可以求逆序对,也是在做题中学会的). 首 ...

  7. szu 寒训第二天 树状数组 二维树状数组详解,以及树状数组扩展应用【求逆序对,以及动态第k小数】

    树状数组(Binary Index Tree) 树状数组可以解决可以转化为前缀和问题的问题 这是一类用以解决动态前缀和的问题 (有点像线段树简版) 1.对于 a1 + a2 + a3 + - + an ...

  8. BZOJ 2431: [HAOI2009]逆序对数列【DP】

    2431: [HAOI2009]逆序对数列 Time Limit: 5 Sec Memory Limit: 128 MB Description 对于一个数列aiai,如果有i<ji<j且 ...

  9. 牛客练习赛33 D tokitsukaze and Inverse Number (树状数组求逆序对,结论)

    链接:https://ac.nowcoder.com/acm/contest/308/D 来源:牛客网 tokitsukaze and Inverse Number 时间限制:C/C++ 1秒,其他语 ...

最新文章

  1. 全卷积神经网路【U-net项目实战】U-net网络结构为什么在医学影像分割上表现不错
  2. 电脑重启后python导入的库不见_为什么python不会在启动时自动导入每个模块?
  3. android 将IE设为默认打开的浏览器
  4. java 根据类名示例化类_Java MathContext类| 带示例的getRoundingMode()方法
  5. 面试官系统精讲Java源码及大厂真题 - 34 只求问倒:连环相扣系列锁面试题
  6. Integer类的toBinaryString源码分析
  7. 三元组法矩阵加法java_C语言实现矩阵加法、减法、乘法和数乘运算
  8. web python template injection_XCTF Web_python_template_injection
  9. typedef int Myfunc(const char *,const struct stat *,int)
  10. Win10屏幕自带的截图,同时保存多个截图
  11. 混合云架构下的安全风险分析和安全解决方案建议
  12. Linux应用开发【第十四章】CAN编程应用开发
  13. 洛谷P【P5708】三角形面积java写法
  14. mysql 1205 解决_mysql 1205 ,自动重启数据库
  15. dva的用法_dva 的基本用法
  16. 调用百度自然语言接口实现文本分析
  17. Windows 7 64 位操作系统安装 Ubuntu 17.10
  18. 小程序开发之页面布局
  19. 2020-12-12
  20. 英勇地死去 VS 卑贱地活着

热门文章

  1. MBA-day22 至多至少问题
  2. java cos90,Java Math.cosh() 方法
  3. Graphite详解
  4. “spoolsv.exe应用程序错误”的解决方法
  5. 远程连接mysql2005_数据库2005链接远程
  6. 初识Memcache---(2)使用memcache
  7. iframe vue 宽度_vue 如何自适应调整嵌入的 iframe 的大小,让用户完全感觉不出有 iframe 这个东西?...
  8. Quectel EC200A-CN移植
  9. 笔记本 ubuntu18.04双系统
  10. 灵性图书馆:好书推荐-《巫士唐望的教诲》