杭电1394 Minimum Inversion Number(线段树+归并排序解法)

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;
} <pre name="code" class="cpp">/*
归并排序解法 加油!!!ans全局变量没有归0  wa了一次。
2014-11-2 12:55 更新
*/
#include<cstdio>
#include<cstring>
#include<climits>
#include<algorithm>
using namespace std;
#define MAX 5050
#define INF 1<<25
int a[MAX],b[MAX],N;
int t1[MAX],t2[MAX],ans;
void Init(){
	for(int i=0;i<=N;i++)
	a[i]=b[i]=INF;
}
void merge(int start,int end,int mid){
	int i,j,k;
	for(k=0,i=start;i<=mid;i++){
		t1[k++]=a[i];
	} t1[k]=INF;
	
	for(k=0,i=mid+1;i<=end;i++){
		t2[k++]=a[i];
	}t2[k]=INF;
	i=j=0;
	for(k=start;k<=end;k++){
		if(t1[i]<=t2[j]){
			a[k]=t1[i++];
		}else{
			a[k]=t2[j++];
			ans+=(mid-start+1-i);
		}
	}
}
void merge_Sort(int start,int end){
	if(end>start){
		int mid=(start+end)>>1;
		merge_Sort(start,mid);
		merge_Sort(mid+1,end);
		merge(start,end,mid);
	}
}
int main(){
	int i,j;
	while(scanf("%d",&N)!=EOF){
			Init();
		ans=0;
		for(i=0;i<N;i++){
			scanf("%d",&a[i]);
			b[i]=a[i];
		}
		merge_Sort(0,N-1);
		int res=ans;ans=INF;
		for(i=0;i<N;i++){
			res=res-b[i]+(N-1-b[i]);
			ans=ans<res?ans:res; 
		}
		printf("%d\n",ans);
	}
return 0;
} 

 
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值