码迷,mamicode.com
首页 > 其他好文 > 详细

杭电1394 Minimum Inversion Number(线段树)

时间:2014-08-26 17:23:36      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   os   java   io   for   ar   2014   

Minimum Inversion Number

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1
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
/*
郁闷……线段树总是错都不知道哪儿错了 渣渣备注了半天结果发现抄都抄不对
加油!!!
Time:2014-8-26 16:52
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
const int MAX=5000+10;
struct Tree{
	int L;
	int R;
	int cnt;
}tree[MAX<<2];//四倍即可 
void BuildTree(int rt,int L,int R){
	tree[rt].L=L;
	tree[rt].R=R;
	tree[rt].cnt=0;
	if(tree[rt].L==tree[rt].R){
		return ;
	}
	int mid=((L+R)>>1);//L+R传入的 不是tree[rt].L 
	
	BuildTree(L(rt),L,mid);
	BuildTree(R(rt),mid+1,R);
} 
void Modify(int rt,int pos){
	if(tree[rt].L==pos&&tree[rt].R==pos){
		tree[rt].cnt++;//可以直接等于 1 即用 1标记 
		return ;
	}
	int mid=((tree[rt].L+tree[rt].R)>>1);
	if(pos<=mid){
		Modify(L(rt),pos);
	}else if(pos>mid){
		Modify(R(rt),pos);
	}
	
	tree[rt].cnt=tree[L(rt)].cnt+tree[R(rt)].cnt;
}
int Query(int rt,int L,int R){
	if(tree[rt].L==L&&tree[rt].R==R){
		return tree[rt].cnt;
	}
	int mid=((tree[rt].L+tree[rt].R)>>1);
	if(R<=mid){
		return Query(L(rt),L,R);
	}else if(L>mid){ 
		return Query(R(rt),L,R);
	}else{
		return Query(L(rt),L,mid)+Query(R(rt),mid+1,R);
	} 
}
void solve(){
	int N;
	int a[MAX];
	while(scanf("%d",&N)!=EOF){
		BuildTree(1,0,N); 
		int sum=0;
		for(int i=1;i<=N;i++){
			scanf("%d",&a[i]);
			sum+=Query(1,a[i],N);//询问从 a[i]+1到 n 中的值的个数。即比 a[i]大的即可与a[i]构成 
			//即比 a[i] 大的值的所有逆序对之和 
			Modify(1,a[i]);//修改的 只需知道位置即可,根节点为1 传入根节点和位置即可 增量 1
		}
		
		int ans=sum;
		for(int i=1;i<=N;i++){
			sum=sum-a[i]+(N-a[i]-1);//因为输入 0 到  n-1,比 a[i]小的 就有 a[i]个 
			//一共 n个数,把第一个移到后边则它增加 的逆序对数为总数 n它减少的逆序对数再减掉它本身 
			// 比如 3 6 9 0 8 5 7 4 2 1 把 3移到后边就应该把 由 3组成的逆序对数减掉 即比 3 小的个数
			//移到最后 一位后,由3增加的逆序对数个数  为比他大的数构成,之和加上它本身刚好为 n 
			ans=min(ans,sum);
		}
		printf("%d\n",ans);
	}
}
int main(){
	solve();
return 0;
} 

杭电1394 Minimum Inversion Number(线段树)

标签:des   style   blog   os   java   io   for   ar   2014   

原文地址:http://blog.csdn.net/u013634213/article/details/38850041

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!