Minimum Inversion Number

本文介绍了一个关于求解序列通过不同轮转方式得到的多个序列中具有最小逆序数的问题,并提供了一段C++代码实现,用于解决此类问题。通过对输入序列的不同轮转,计算每种情况下序列的逆序数,并找到逆序数最小的情况。

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

Problem Description
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj. For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of the seqence, we will obtain another sequence. There are totally n such sequences as the following: a1, a2, ..., an-1, an (where m = 0 - the initial seqence) a2, a3, ..., an, a1 (where m = 1) a3, a4, ..., an, a1, a2 (where m = 2) ... an, a1, a2, ..., an-1 (where m = n-1) You are asked to write a program to find the minimum inversion number out of the above sequences.
 

Input
The input consists of a number of test cases. Each case consists of two lines: the first line contains a positive integer n (n <= 5000); the next line contains a permutation of the n integers from 0 to n-1.
 

Output
For each case, output the minimum inversion number on a single line.
 

Sample Input
10 1 3 6 9 0 8 5 7 4 2
 

Sample Output
16
 

Author
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define MAX 5050
using namespace std;
struct Node
{
    int l,r,sum;
}node[MAX<<2];
void build_tree(int l,int r,int root)
{
    node[root].l=l;
    node[root].r=r;
    if(l==r)
    {
        node[root].sum=0;
        return ;
    }
    int mid=(l+r)>>1;
    build_tree(l,mid,root<<1);
    build_tree(mid+1,r,(root<<1)|1);
    node[root].sum=0;
}
void add(int i,int t,int val)
{
    node[i].sum+=val;
    if(node[i].l==node[i].r){
        return ;
    }
    int mid=(node[i].l+node[i].r)>>1;
    if(t<=mid)add(i<<1,t,val);
    else add((i<<1)|1,t,val);
}
int sum(int l,int r,int root)
{
    if(node[root].l==l&&node[root].r==r)
        return node[root].sum;
    int mid=(node[root].r+node[root].l)>>1;
    if(r<=mid)return sum(l,r,root<<1);
    else if(l>mid)return sum(l,r,(root<<1)|1);
    else
    {
        return sum(l,mid,root<<1)+sum(mid+1,r,(root<<1)|1);
    }
}
int a[MAX];
int main()
{
  int n;
  while(~scanf("%d",&n))
  {
      build_tree(1,n,1);
      for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
      int ans=0;
      for(int i=1;i<=n;i++)
      {
          ans+=sum(a[i]+1,n,1);
          add(1,a[i]+1,1);
      }
      int Min=ans;
      for(int i=1;i<=n;i++){
        ans-=a[i];
        ans+=n-a[i]-1;
        if(ans<Min)Min=ans;
      }
      printf("%d\n",Min);
  }
  return 0;
}


<think>我们参考引用资料,特别是引用[2]和引用[4]提到可以使用归并排序(mergesort)的思想来统计逆序对,时间复杂度为O(nlogn)。算法思想:在归并排序的过程中,当合并两个已排序的子数组时,如果左边子数组的元素大于右边子数组的元素,那么左边子数组中该元素及其后面的所有元素都会与右边子数组的该元素构成逆序对。具体步骤:1.分割:将数组分成左右两半,递归地对左右两半进行排序并统计逆序对的数量。2.合并:在合并过程中,对于左右两个子数组,使用两个指针分别指向左子数组和右子数组的起始位置。比较两个指针指向的元素,如果左指针指向的元素小于等于右指针指向的元素,则将该元素放入合并后的数组,左指针右移;如果左指针指向的元素大于右指针指向的元素,则说明左指针当前元素及其后面的所有元素(因为左子数组已排序)都与右指针当前元素构成逆序对,因此逆序对数量增加(左子数组剩余元素的数量),然后将右指针当前元素放入合并后的数组,右指针右移。3.将合并过程中统计的逆序对数量加上左右子数组递归统计的逆序对数量,即为总的逆序对数量。注意:在合并过程中,当左子数组元素大于右子数组元素时,逆序对增加的数量是左子数组中从当前指针到末尾的元素个数(即mid-i+1,其中i是左子数组当前指针,mid是左子数组的末尾索引)。我们来实现这个算法。注意数组索引范围,这里我们使用闭区间。</think>### 计算数组逆序对的算法 逆序对(Inversion Pair)是指数组中满足 $i < j$ 但 $a[i] > a[j]$ 的元素对。统计逆序对数量在数据分析、排序算法评估等领域有重要应用。以下基于归并排序的分治算法可实现 $O(n \log n)$ 的时间复杂度: #### 算法核心思想 在归并排序过程中统计逆序对: 1. **分治**:将数组递归拆分为左右子数组 2. **合并统计**:合并有序子数组时,当左子数组元素 $a[i] > a[j]$($j$ 属于右子数组),则左子数组 $i$ 之后的所有元素都与 $a[j]$ 构成逆序对 #### Python 实现 ```python def count_inversions(arr): def merge_sort_count(arr, left, right): if left >= right: return 0 mid = (left + right) // 2 count = 0 count += merge_sort_count(arr, left, mid) # 左子数组逆序对 count += merge_sort_count(arr, mid + 1, right) # 右子数组逆序对 count += merge(arr, left, mid, right) # 合并过程统计 return count def merge(arr, left, mid, right): temp = [] i, j = left, mid + 1 count = 0 while i <= mid and j <= right: if arr[i] <= arr[j]: temp.append(arr[i]) i += 1 else: temp.append(arr[j]) j += 1 # 左子数组剩余元素均与arr[j]构成逆序对 count += (mid - i + 1) while i <= mid: temp.append(arr[i]) i += 1 while j <= right: temp.append(arr[j]) j += 1 arr[left:right+1] = temp return count return merge_sort_count(arr, 0, len(arr) - 1) ``` #### 算法分析 1. **时间复杂度**:$O(n \log n)$ - 归并排序的递归深度为 $\log n$,每层合并操作总代价为 $O(n)$ 2. **空间复杂度**:$O(n)$(归并临时数组) 3. **正确性验证**: 当 $a[i] > a[j]$ 时,左子数组 $[i, mid]$ 区间所有元素均大于 $a[j]$,故逆序对增加 $(mid - i + 1)$[^2] > **示例计算**: > 数组 `[2, 4, 6, 1, 3, 5]` 的逆序对: > $(2,1), (4,1), (4,3), (6,1), (6,3), (6,5)$ → 共6个逆序对[^4] #### 变种问题扩展 1. **重要逆序对**(Significant Inversion): 若要求满足 $a[i] > 3 \times a[j]$ 且 $i<j$,可在归并比较时修改条件为 `left_arr[i] > 3 * right_arr[j]`,统计方式不变[^3] 2. **多维数据**: 可通过嵌套归并排序处理更高维度的逆序对统计 #### 应用场景 - 衡量数据有序度(如协作过滤推荐系统) - 基因序列相似性分析 - 金融数据分析(如订单簿不平衡度) ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值