线段树之逆序数

求逆序数的方法:
1.直接暴力求解
2.线段树求解
需要知道HASH(离散化)。//192和132逆序数是一样的,所以可以映射到一样的情况。
求逆序数的思路:遍历每一个数字,如果这个数字前面有比它大的数字,那么比它大的数字的个数即比他大的区间的sum值,例如a=5,它的逆序数即6-10的sum值。
例题:

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
本题思路:因为这个序列的数字是从0~n-1并且是没有重复的数字,所以也就不需要离散化了,就遍历输入的序列即可,因为后面的序列的逆序数是ans-a[0]+n-a[n]-1;因为最前面的数字到了最后面,那么假如n=10,a[0]=3,那么在后面比它小的数字只有0、1、2,所以它到了最后面逆序数-3,而比它大的有3、4、5、6、7、8、9所以又多了6,之后以此类推。

//有问题。。。。
#include <cstdio>
#include <iostream>
#include <string.h>
#define MAXSIZE 5005
using namespace std;
int N;
int number[MAXSIZE];
int mini[MAXSIZE];
int C;
struct node{
	int l,r;
	int sum;
}tr[MAXSIZE<<2];
void build(int m,int l,int r);
void pushup(int m);
void update(int m,int index,int val);//单点更新
int query(int m,int l,int r);//区间查询

int main(){
	int cnt;
	while(~scanf("%d",&N)){
		memset(tr,0,sizeof(tr));
		C=0;
		cnt=0;//求这个点的逆序数
		build(1,0,N-1);//建树过程
		for(int i=0;i<N;i++){
			scanf("%d",&number[i]);
			update(1,number[i],1);
			if(number[i]+1<=N-1){//需要考虑是否越界
				cnt+=query(1,number[i]+1,N-1);
			}
		}
		mini[C++]=cnt;//计算每个序列的逆序数
		for(int i=0;i<N-1;i++){//意思是把number[i]移到序列最后面
			mini[C]=mini[C-1]-number[i]+N-number[i]-1;
			C++;
		}
		int Min =100000000;
		for(int i=0;i<C;i++){
			if(mini[i]<Min)Min = mini[i];
		}
		printf("%d\n",Min);
	}
	return 0;
}
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 update(int m,int index,int val){
	if(tr[m].l==index&&tr[m].r==index){
		tr[m].sum+=val;
		return;
	}
	int mid = (tr[m].r+tr[m].l)>>1;
	if(index>mid)update(m<<1|1,index,val);
	else if(index<=mid)update(m<<1,index,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].r+tr[m].l)>>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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值