hdu1394 Minimum Inversion Number(线段树+单点更新)

Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23159    Accepted Submission(s): 13765

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

CHEN, Gaoli

Source

ZOJ Monthly, January 2003 

题意:求数组中一共有多少组逆序数对,数组可以变化,每次把第一个数放在数组最后面,求其中出现的最少的逆序数对。

思路:先考虑原来的数组,例如案例一。数组为:1 3 6 9 0 8 5 7 4 2

当数组中元素只有1时,逆序对为0

当数组中元素只有1和3时,3前面的数小于3,逆序对为0

当数组中元素只有1、3、6时,6前面的数小于6,逆序对为0

当数组中元素只有1、3、6、9时,9前面的数小于9,逆序对为0

当数组中元素只有1、3、6、9、0时,0前面存在1,3,6,9,逆序对为4

当数组中元素只有1、3、6、9、0、8时,8前面存在9,逆序对为4+1

.。。。

因此我们可以每次插入一个数a[i]、然后统计之前插入且比a[i]大的数有多少个。

使用线段树可以实现区间的查询操作。

建好树后,每次逆序对的值加上树[a[i]+1,n-1]的值,然后将当前的a[i]插入到树中。

对于数组的每次把第一个数放到最后的操作:

比a[i]小的数有a[i]个,比a[i]大的数有n-a[i]-1个,因此

逆序对的值+=(n-a[i]-1)-a[i];

AC代码:

#include <stdio.h>
#include <algorithm>
typedef long long ll;
const int MAX_N = 1e6 + 100;
using namespace std;
int n,m;
int a[MAX_N];
struct node{
	int l,r,lazy;
	ll sum;
	void update(ll x){
		sum+=1ll*(r-l+1)*x;
		lazy+=x;
	}
}tree[MAX_N>>2];

void push_up(int x){
	tree[x].sum=tree[x<<1].sum+tree[x<<1|1].sum;
}

void push_down(int x){
	int lazyvalue=tree[x].lazy;
	if(lazyvalue>0){
		tree[x<<1].update(lazyvalue);
		tree[x<<1|1].update(lazyvalue);
		tree[x].lazy=0;
	}
}

void build(int x,int l, int r){
	tree[x].l=l;tree[x].r=r;
	tree[x].sum=0;tree[x].lazy=0;
	if(l==r){
		tree[x].sum=0;
		return ;
	}
	int mid=(l+r)>>1;
	build(x<<1,l,mid);
	build(x<<1|1,mid+1,r);
	push_up(x);
}

void update(int x,int l,int r,ll val){
	int L=tree[x].l;int R=tree[x].r;
	int mid=(L+R)>>1;
	if(l<=L && R<=r){
		tree[x].update(val);
		return ;
	}
	push_down(x);
	if(l<=mid){
		update(x<<1,l,r,val);
	}
	if(mid<r){
		update(x<<1|1,l,r,val);
	}
	push_up(x);	
}

ll query(int x,int l,int r){
	int L=tree[x].l;int R=tree[x].r;
	int mid=(L+R)>>1;
	if(l<=L && R <=r){
		return tree[x].sum;
	}
	push_down(x);
	ll ans=0;
	if(l<=mid){
		ans+=query(x<<1,l,r);
	}
	if(mid<r){
		ans+=query(x<<1|1,l,r);
	}
	return ans;
}

int main(){
	while(~scanf("%d",&n)){
		for(int i=0;i<n;i++){
			scanf("%d",&a[i]);
		}
		build(1,0,n-1);
		ll ans=0;
		for(int i=0;i<n;i++){
			ans+=query(1,a[i]+1,n-1);
			update(1,a[i],a[i],1);
		}
		ll tmp=ans;
	//	printf("ans=%lld\n",tmp);
		for(int i=0;i<n;i++){
			tmp=tmp+n-a[i]-1-a[i];
	//			printf("%lld\n",tmp);
			ans=min(tmp,ans);
		}
		printf("%lld\n",ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Rainbow_storm/article/details/80472218
今日推荐