Minimum Inversion Number (线段树解决逆序数问题)

本文介绍了一种利用线段树数据结构解决逆序数问题的方法,具体为找到给定序列经旋转后的所有可能序列中逆序数最小的一个。通过构建线段树并进行更新和查询操作,算法能在O(n log n)的时间复杂度内解决问题。

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

Minimum Inversion Number

线段树解决逆序数问题

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
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=5005;
struct node{
	int sum,l,r;
}tr[maxn<<2];
int x[maxn];
void pushup(int m){
	tr[m].sum=tr[m<<1].sum+tr[m<<1|1].sum;
}
void build(int m,int l,int r){
	tr[m].l=l;
	tr[m].r=r;
	if(l==r){
		tr[m].sum=0;
		return ;
	}
	int mid=(l+r)>>1;
	build(m<<1,l,mid);
	build(m<<1|1,mid+1,r);
	pushup(m);
}
void updata(int m,int inx,int val){
	if(tr[m].l==inx&&tr[m].r==inx){
		tr[m].sum+=val;
		return ;
	}
	int mid=(tr[m].l+tr[m].r)>>1;
	if(inx<=mid) updata(m<<1,inx,val);
	else updata(m<<1|1,inx,val);
	pushup(m);
}
int query(int m,int l,int r){
	if(tr[m].l==l&&tr[m].r==r){
		return tr[m].sum;
	}
	int mid=(tr[m].l+tr[m].r)>>1;
	int temp;
	if(r<=mid) temp=query(m<<1,l,r);
	else if(l>mid) temp=query(m<<1|1,l,r);
	else temp=query(m<<1,l,mid)+query(m<<1|1,mid+1,r);
	return temp;
}
int main(){
	int n;
	while(~scanf("%d",&n)){
		build(1,0,n);
		int sum=0;
		for(int i=0;i<n;i++){
			scanf("%d",&x[i]);
		    sum+=query(1,x[i]+1,n);
			updata(1,x[i],1);	
		}	
		int min=sum;
		for(int i=0;i<n-1;i++){
			sum+=n-1-x[i]-x[i];
			if(sum<min) min=sum;
		}
		printf("%d\n",min);
	}
	return 0;
}

在C语言中,我们可以使用分治法来解决逆序对(也称作“下降对”)的问题,这是一个经典的计算机科学问题,通常用于评估数组元素排列的混乱程度。给定一个整数数组,我们需要找到其中有多少对元素满足第一个元素大于第二个元素的条件。 一种常见的解决策略是归并排序的思想,因为归并排序过程中会自动计算出逆序对的数量。以下是简单的步骤: 1. **分割**(Divide): 将原始数组一分为二,直到每个子数组只有一个元素。 2. **合并**(Combine): 对两半数组进行合并,同时统计逆序对。当比较两个元素时,如果前一个元素大于后一个,就增加一个逆序对计数。 3. **合并返回**: 把合并后的结果递归地应用到剩余的子数组上。 以下是一个简化版的C语言代码示例: ```c #include <stdio.h> int merge(int arr[], int l, int m, int r) { int i, j, k = 0; int n1 = m - l + 1; int n2 = r - m; int L[n1], R[n2]; for (i = 0; i < n1; i++) L[i] = arr[l + i]; for (j = 0; j < n2; j++) R[j] = arr[m + 1+ j]; i = 0; j = 0; while (i < n1 && j < n2) { if (L[i] > R[j]) { arr[k++] = R[j++]; } else { arr[k++] = L[i++]; } } while (i < n1) arr[k++] = L[i++]; while (j < n2) arr[k++] = R[j++]; return k; } void countInversions(int arr[], int l, int r) { if (l >= r) return; int mid = l + (r - l) / 2; countInversions(arr, l, mid); countInversions(arr, mid + 1, r); int inversionsInLeft = countInversions(arr, l, mid); int inversionsInRight = countInversions(arr, mid + 1, r); int mergedInvCount = inversionsInLeft + inversionsInRight; int temp[2 * mid + 1]; merge(arr, l, mid, r, temp); // 更新左半边和右半边的逆序对数 inversionsInLeft += merge(temp, l, mid, mid, arr); inversionsInRight += merge(temp, mid + 1, r, 2 * mid + 1, arr); return inversionsInLeft + inversionsInRight; } int main() { int arr[] = {2, 4, 1, 3, 5}; int n = sizeof(arr) / sizeof(arr[0]); printf("Number of inversion pairs is %d\n", countInversions(arr, 0, n - 1)); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值