当前位置:网站首页>归并排序求逆序数
归并排序求逆序数
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;
}
边栏推荐
- The interview questions and answers for the high-frequency software test of Dachang help you prepare for the golden nine silver ten
- 卷积与反卷积关系超详细说明及推导(反卷积又称转置卷积、分数步长卷积)
- 2022安全员-C证考试模拟100题及在线模拟考试
- LLVM TargetPassConfig
- 2022年全国最新消防设施操作员(高级消防设施操作员)模拟题及答案
- 对技术的乐观,正让戴尔取得比想象中更多的成就
- A website for programmers with a monthly salary of 30K
- 启动服务11111
- Which securities company should I choose to open an account online? Is it safe to open an account online?
- 4 ans d'expérience de travail, 5 modes de communication Multi - thread ne peuvent pas être décrits, vous osez croire?
猜你喜欢

If the order has not been paid for 30 minutes, it will be automatically cancelled. How can I achieve this?

4年工作经验,多线程间的5种通信方式都说不出来,你敢信?

The basic principle and application of iterator and enhanced for

汇编语言(3)16位汇编基础框架与加减循环

Use of file class filenamefilter & filefilter in io

4年工作經驗,多線程間的5種通信方式都說不出來,你敢信?

Databinding quick start (still using findviewbyid?)

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

108页(4万字)未来公寓智能化设计平台项目方案建议书2022版

Text editor of QT project practice ---------- episode 11
随机推荐
ContentResolver,拿到手机短信内容
重磅:国产IDE发布,由阿里研发,完全开源!(高性能+高定制性)
图片旋转移动缩放渐变
图书馆管理系统代码源码(php+css+js+mysql) 完整的代码源码
网上开户选哪个证券公司?网上开户安全么?
腾讯云WeCity丨产业联合 协同创新 共贺新春!
扎克伯格上手演示四款VR头显原型机,Meta透露元宇宙「家底」
Ecological escort cloud service providers wave "Intel flag"
Super detailed description and derivation of convolution and deconvolution (deconvolution is also called transpose convolution and fractional step convolution)
Novice, let me show you the "soft test" at one time
新手看过来,带你一次性了解“软考”
Lenovo tongfuyao: 11 times the general trend, we attacked the city and pulled out the stronghold all the way
指南针炒股软件怎么样?安全吗?
[micro service sentinel] real time monitoring | RT | throughput | concurrency | QPS
【实用系列】家内wifi全覆盖
Previous basic review (link)
Programmer: did you spend all your savings to buy a house in Shenzhen? Or return to Changsha to live a "surplus" life?
用手机在同花顺上开户靠谱吗?这样炒股有没有什么安全隐患
Scala sample class
2022r1 quick opening pressure vessel operation test questions and answers