分治算法题:nlgn时间复杂度计算原序列的重要逆序个数

本文介绍了如何使用分治算法和归并排序在O(nlogn)的时间复杂度内计算序列中的显著逆序对数量。显著逆序对是指满足ai>2aj条件的数对。与常规逆序对不同,该问题在合并子序列时需要分别计算重要逆序数和进行排序,通过调整归并排序的合并过程来实现这一目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述:

Recall the problem of finding the number of inversions. As in the text, 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> 2aj. Given an O(nlogn) algorithm to count the number of significant inversions between two orderings.

我的理解:让我们求一个序列的significant inversion个数,这个不同于一般的逆序数,但是求的思想是一样的,都是采用分治法和归并排序的算法实现。但是不同于普通的求逆序数算法,如果是求ai>aj的个数的话,可以在计算合并子序列的逆序数个数的过程中顺便排序。因为如果不满足ai>aj的话,那就将aj放入tmp序列中,继续比较ai和a(j+1)的大小。

在思考多久之后,决定在函数merge的实现中,将计算合并的过程中产生的重要逆序数和给这个合并子序列排序分开用两个while循环实现。具体实现看代码。

#include<iostream>
#include<stdio.h>
#include
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值