当前位置:网站首页>归并排序求逆序数
归并排序求逆序数
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;
}
边栏推荐
- adb shell getevent
- Use of file class filenamefilter & filefilter in io
- 4 ans d'expérience de travail, 5 modes de communication Multi - thread ne peuvent pas être décrits, vous osez croire?
- JS Chapter 1 Summary
- ImageView shows network pictures
- 【实用系列】家内wifi全覆盖
- Text border format and text block of rich text
- 108 pages (40000 words) proposal for future apartment intelligent design platform project (version 2022)
- Scala IO reads by lexical units and numbers
- 2022r1 quick opening pressure vessel operation test questions and answers
猜你喜欢

2022年危险化学品经营单位安全管理人员考试试题及模拟考试

108页(4万字)未来公寓智能化设计平台项目方案建议书2022版
![[microservices sentinel] sentinel quick start | building an image | starting the console](/img/88/a01c8120f6117f1b8e4463cf6f685f.png)
[microservices sentinel] sentinel quick start | building an image | starting the console

Examination questions and mock examination for safety management personnel of hazardous chemical business units in 2022

15.线程同步的几种方法

扎克伯格上手演示四款VR头显原型机,Meta透露元宇宙「家底」
The latest QQ wechat domain name anti red PHP program source code + forced jump to open

JS Chapter 1 Summary

使用 Loki 微服务模式部署生产集群

4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
随机推荐
VB learning notes
Leetcode 1248. 统计「优美子数组」(害,突然发现只会暴力枚举了)
A small crawler program written by beginners
移动安全工具-dex2jar
Première application de l'informatique quantique à la modélisation des flux de puissance dans les systèmes énergétiques à l'Université technique danoise
4 ans d'expérience de travail, 5 modes de communication Multi - thread ne peuvent pas être décrits, vous osez croire?
汇编语言(4)函数传参
2022 simulated 100 questions of safety officer-c certificate examination and online simulated examination
腾讯云WeCity丨你好 2022!
我想问一下兴业证券怎么开户?通过链接办理股票开户安全吗
Default methods for Scala sample classes
Golang示例续期锁:Redis+Channel+sync.Mutex
Deploy a production cluster using Loki microservice pattern
MySQL common basic statements (collation)
If the order has not been paid for 30 minutes, it will be automatically cancelled. How can I achieve this?
2022年危险化学品经营单位安全管理人员考试试题及模拟考试
移动安全工具-apktool
Scala IO reads by lexical units and numbers
Preliminary understanding of qtoolbutton
Scala responsibility chain pattern