当前位置:网站首页>归并排序求逆序数
归并排序求逆序数
2022-06-24 20:32:00 【GHOSTANDBREAD】
逆序数:
例如:2 3 4 5 6 1
可以发现2和1, 3和1, 4和1, 5和1, 6和1都是逆序所以逆序对数量为5
不难发现逆序对数量即为一串乱序的数变为有序,所要经过两两交换的次数,即乱序的数到它有序时的位置的距离
求两两交换的次数,可以想到用冒泡排序去求,但是冒泡排序复杂度为n^2,数据量大时就不能用。例如数据量为1e6时,就不行了,所以可以用归并排序,在分治归并的过程中求出交换的次数(距离),时间复杂度为nlogn
思路:
归并排序求逆序数,我们发现只需在归并时进行一下判断即可。
归并时有两种情况,①a[i] <= a[j] 和 ②a[i] > a[j] ,第一种情况即左区间的数小于右区间的数,则没有逆序,第二种情况是出现了逆序,不难发现,应交换的次数(距离)为第一个区间还剩的数的数量,即mid - i + 1, 则通过归并之后,我们可以得到逆序对数量
代码:
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
int n;
ll res;
const int N = 1e6 + 10;
vector<int> a(N);
vector<int> tmp(N);
void solve(vector<int> &a, int l, int r) {
if(l >= r) return;
int mid = (l + r) >> 1;
solve(a, l, mid);
solve(a, mid + 1, r);
int k = 0, i = l, j = mid + 1;
while(i <= mid && j <= r) {
if(a[i] <= a[j]) tmp[k ++] = a[i ++];
else {
tmp[k ++] = a[j ++];
res += (mid - i + 1); // 求交换距离
}
}
while(i <= mid) tmp[k ++] = a[i ++];
while(j <= r) tmp[k ++] = a[j ++];
for(i = l, j = 0; i <= r; i ++, j ++) a[i] = tmp[j];
}
int main() {
ios::sync_with_stdio(false);
cout.tie(nullptr);
cin >> n;
for(int i = 0; i < n; i ++) cin >> a[i];
solve(a, 0, n - 1);
cout << res;
return 0;
}
边栏推荐
- 51单片机多机通信
- QT(35)-操作EXCEL-QXlsx-QAxObject
- Previous basic review
- 2022年起重机司机(限桥式起重机)考试题库模拟考试平台操作
- Can communication experiment between C and C
- Scala sample class case calculate
- JS Chapter 1 Summary
- 腾讯完成全面上云 打造国内最大云原生实践
- A website for programmers with a monthly salary of 30K
- Thermodynamic diagram display correlation matrix
猜你喜欢

2022年起重机司机(限桥式起重机)考试题库模拟考试平台操作

Cobalt Strike安装教程

QT(35)-操作EXCEL-QXlsx-QAxObject

Rich text tables, lists, pictures

程序员:是花光积蓄在深圳买房?还是回到长沙过“富余”生活?

108 pages (40000 words) proposal for future apartment intelligent design platform project (version 2022)

51单片机多机通信
![[redis realizes seckill service ②] solution to oversold problem](/img/b6/3073def06a56565894c28e49767e3e.png)
[redis realizes seckill service ②] solution to oversold problem

Scala IO read by line

利用 Redis 的 sorted set 做每周热评的功能
随机推荐
LLVM TargetPassConfig
Go语言运算符(第8课下)
中金财富证券开户佣金多少呢?股票开户交易安全靠谱吗?
Novice, let me show you the "soft test" at one time
ContentResolver,拿到手机短信内容
I'd like to ask how to open an account at industrial securities? Is it safe to open a stock account through the link
Heavyweight: the domestic ide was released, developed by Alibaba, and is completely open source! (high performance + high customization)
Lenovo tongfuyao: 11 times the general trend, we attacked the city and pulled out the stronghold all the way
网上开户选哪个证券公司?网上开户安全么?
2022年危险化学品经营单位安全管理人员考试试题及模拟考试
The basic principle and application of iterator and enhanced for
Scala IO reads by lexical units and numbers
戴尔为何一直拒绝将商用本的超薄推向极致?
Scala adapter pattern
JS Chapter 1 Summary
Which securities company should I choose to open an account online? Is it safe to open an account online?
2022安全员-C证考试模拟100题及在线模拟考试
[practical series] full WiFi coverage at home
[micro service sentinel] real time monitoring | RT | throughput | concurrency | QPS
51单片机多机通信